<!DOCTYPE html>


<html lang="en">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
     
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      
<section class="cover">
    
  <div class="cover-frame">
    <div class="bg-box">
      <img src="/images/82752296_p0.jpg" alt="image frame" />
    </div>
    <div class="cover-inner text-center text-white">
      <h1><a href="/">Doreen&#39;s Blog</a></h1>
      <div id="subtitle-box">
        
        <span id="subtitle"></span>
        
      </div>
      <div>
        
      </div>
    </div>
  </div>
  <div class="cover-learn-more">
    <a href="javascript:void(0)" class="anchor"><i class="ri-arrow-down-line"></i></a>
  </div>
</section>



<script src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js"></script>


<!-- Subtitle -->

  <script>
    try {
      var typed = new Typed("#subtitle", {
        strings: ['一切都会好的，城南的花都开了', '今晚的月色真美', '注重过程，别在乎结果'],
        startDelay: 0,
        typeSpeed: 200,
        loop: true,
        backSpeed: 100,
        showCursor: true
      });
    } catch (err) {
      console.log(err)
    }
  </script>
  
<div id="main">
  <section class="outer">
  
  

<div class="notice" style="margin-top:50px">
    <i class="ri-heart-fill"></i>
    <div class="notice-content" id="broad"></div>
</div>
<script type="text/javascript">
    fetch('https://v1.hitokoto.cn')
        .then(response => response.json())
        .then(data => {
            document.getElementById("broad").innerHTML = data.hitokoto;
        })
        .catch(console.error)
</script>

<style>
    .notice {
        padding: 20px;
        border: 1px dashed #e6e6e6;
        color: #969696;
        position: relative;
        display: inline-block;
        width: 100%;
        background: #fbfbfb50;
        border-radius: 10px;
    }

    .notice i {
        float: left;
        color: #999;
        font-size: 16px;
        padding-right: 10px;
        vertical-align: middle;
        margin-top: -2px;
    }

    .notice-content {
        display: initial;
        vertical-align: middle;
    }
</style>
  
  <article class="articles">
    
    
    
    
    <article
  id="post-vue3"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/10/09/vue3/"
    >vue3.0</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/10/09/vue3/" class="article-date">
  <time datetime="2020-10-09T09:30:00.000Z" itemprop="datePublished">2020-10-09</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/vue/">vue</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <blockquote>
<p>结合<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV14k4y117LL">李江南老师的视频</a>和<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1L5411j7vj">技术胖的视频</a>和文档进行二次编辑的</p>
</blockquote>
<h2 id="Vue3-0亮点"><a href="#Vue3-0亮点" class="headerlink" title="Vue3.0亮点"></a>Vue3.0亮点</h2><ul>
<li>Performance：性能比Vue 2.x快1.2~2倍</li>
<li>Tree shaking support：按需编译,体积比Vue2.x更小</li>
<li>Composition API: 组合API(类似React Hooks)</li>
<li>Better TypeScript support：更好的 Ts 支持</li>
<li>Custom Renderer API：暴露了自定义渲染API</li>
<li>Fragment, Teleport(Protal), Suspense：更先进的组件</li>
</ul>
<h2 id="Vue3-0是如何变快的"><a href="#Vue3-0是如何变快的" class="headerlink" title="Vue3.0是如何变快的"></a>Vue3.0是如何变快的</h2><h3 id="diff算法优化-https-vue-next-template-explorer-netlify-app"><a href="#diff算法优化-https-vue-next-template-explorer-netlify-app" class="headerlink" title="diff算法优化: https://vue-next-template-explorer.netlify.app/"></a>diff算法优化: <a target="_blank" rel="noopener" href="https://vue-next-template-explorer.netlify.app/">https://vue-next-template-explorer.netlify.app/</a></h3><blockquote>
<ul>
<li>Vue2中的虚拟dom是进行全量的对比  </li>
<li>Vue3新增了<code>非静态标记</code>(PatchFlag)  <ol>
<li>在与上次虚拟节点进行对比时候，只对比带有patch flag的节点</li>
<li>并且可以通过flag的信息得知当前节点要对比的具体内容</li>
</ol>
</li>
</ul>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// template内的内容</span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Hello World!<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">:title</span>=<span class="string">&#x27;haha&#x27;</span> <span class="attr">:value</span>=<span class="string">&#x27;hh&#x27;</span>&gt;</span>&#123;&#123;msg&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 源码</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(_Fragment, <span class="literal">null</span>, [</span><br><span class="line">    _createVNode(<span class="string">&quot;div&quot;</span>, <span class="literal">null</span>, <span class="string">&quot;Hello World!&quot;</span>),</span><br><span class="line">    _createVNode(<span class="string">&quot;h1&quot;</span>, &#123;</span><br><span class="line">      title: _ctx.haha,</span><br><span class="line">      value: _ctx.hh</span><br><span class="line">    &#125;, _toDisplayString(_ctx.msg), <span class="number">9</span> <span class="comment">/* TEXT, PROPS */</span>, [<span class="string">&quot;title&quot;</span>, <span class="string">&quot;value&quot;</span>])</span><br><span class="line">  ], <span class="number">64</span> <span class="comment">/* STABLE_FRAGMENT */</span>))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">不需要遍历节点一一对比</span></span><br><span class="line"><span class="comment">对动态的组件进行标记（只比较标记的内容）</span></span><br><span class="line"><span class="comment">数字的不同表示了 比较的内容不同</span></span><br><span class="line"><span class="comment">不需要针对整个节点去改变，只需要针对特定的内容去重新渲染</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">9 ---&gt; 代表他动态的内容为 text 和 props</span></span><br><span class="line"><span class="comment">改变的时候只需要改变文本内容和属性</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h3 id="hoistStatic-静态提升"><a href="#hoistStatic-静态提升" class="headerlink" title="hoistStatic 静态提升"></a>hoistStatic 静态提升</h3><blockquote>
<ul>
<li>Vue2中无论元素是否参与更新, 每次都会重新创建, 然后再渲染</li>
<li>Vue3中对于不参与更新的元素, 会做静态提升, 只会被创建一次, 在渲染时直接复用即可</li>
</ul>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// template内的内容</span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Hello World!<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">:title</span>=<span class="string">&#x27;haha&#x27;</span> <span class="attr">:value</span>=<span class="string">&#x27;hh&#x27;</span>&gt;</span>&#123;&#123;msg&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 源码</span></span><br><span class="line"><span class="comment">// 静态提升之前</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(_Fragment, <span class="literal">null</span>, [</span><br><span class="line">    _createVNode(<span class="string">&quot;div&quot;</span>, <span class="literal">null</span>, <span class="string">&quot;Hello World!&quot;</span>),</span><br><span class="line">    _createVNode(<span class="string">&quot;h1&quot;</span>, &#123;</span><br><span class="line">      title: _ctx.haha,</span><br><span class="line">      value: _ctx.hh</span><br><span class="line">    &#125;, _toDisplayString(_ctx.msg), <span class="number">9</span> <span class="comment">/* TEXT, PROPS */</span>, [<span class="string">&quot;title&quot;</span>, <span class="string">&quot;value&quot;</span>])</span><br><span class="line">  ], <span class="number">64</span> <span class="comment">/* STABLE_FRAGMENT */</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 静态提升之后</span></span><br><span class="line"><span class="keyword">const</span> _hoisted_1 = <span class="comment">/*#__PURE__*/</span>_createVNode(<span class="string">&quot;div&quot;</span>, <span class="literal">null</span>, <span class="string">&quot;Hello World!&quot;</span>, <span class="number">-1</span> <span class="comment">/* HOISTED */</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(_Fragment, <span class="literal">null</span>, [</span><br><span class="line">    _hoisted_1,</span><br><span class="line">    _createVNode(<span class="string">&quot;h1&quot;</span>, &#123;</span><br><span class="line">      title: _ctx.haha,</span><br><span class="line">      value: _ctx.hh</span><br><span class="line">    &#125;, _toDisplayString(_ctx.msg), <span class="number">9</span> <span class="comment">/* TEXT, PROPS */</span>, [<span class="string">&quot;title&quot;</span>, <span class="string">&quot;value&quot;</span>])</span><br><span class="line">  ], <span class="number">64</span> <span class="comment">/* STABLE_FRAGMENT */</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">每次render的时候全部内容会重新创建一次，会造成额外的内存消耗</span></span><br><span class="line"><span class="comment">通过把静态的节点提取为全局变量</span></span><br><span class="line"><span class="comment">所以就只需要创建一次</span></span><br><span class="line"><span class="comment">组件渲染的时候只需要调用就行了</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h3 id="cacheHandlers-事件侦听器缓存"><a href="#cacheHandlers-事件侦听器缓存" class="headerlink" title="cacheHandlers 事件侦听器缓存"></a>cacheHandlers 事件侦听器缓存</h3><ul>
<li>默认情况下onClick会被视为动态绑定, 所以每次都会去追踪它的变化<pre><code>但是因为是同一个函数，所以没有追踪变化, 直接缓存起来复用即可</code></pre>
</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&#x27;handleClick&#x27;</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 开启事件侦听之前</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(<span class="string">&quot;button&quot;</span>, &#123; <span class="attr">onClick</span>: _ctx.handleClick &#125;, <span class="string">&quot;按钮&quot;</span>, <span class="number">8</span> <span class="comment">/* PROPS */</span>, [<span class="string">&quot;onClick&quot;</span>]))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 开启事件侦听之后</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">_ctx, _cache, $props, $setup, $data, $options</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (_openBlock(), _createBlock(<span class="string">&quot;button&quot;</span>, &#123;</span><br><span class="line">    onClick: _cache[<span class="number">1</span>] || (_cache[<span class="number">1</span>] = <span class="function">(<span class="params">...args</span>) =&gt;</span> (_ctx.handleClick(...args)))</span><br><span class="line">  &#125;, <span class="string">&quot;按钮&quot;</span>))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">开启之前，通过静态标记的形式来实现 ==&gt; 比较属性（但是方法是不变的）</span></span><br><span class="line"><span class="comment">开启之后，不使用静态标记，所以不会进行比较。通过一个变量对方法进行缓存，下次在调用的时候直接从缓存中获取，不去再次创建整个函数</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h3 id="ssr渲染"><a href="#ssr渲染" class="headerlink" title="ssr渲染"></a>ssr渲染</h3><ul>
<li><p>当有大量静态的内容时候，这些内容会被当做纯字符串推进一个buffer里面，即使存在动态的绑定，会通过模板插值嵌入进去。这样会比通过虚拟dmo来渲染的快上很多很多。</p>
</li>
<li><p>当静态内容大到一定量级时候，会用_createStaticVNode方法在客户端去生成一个static node，这些静态node，会被直接innerHtml，就不需要创建对象，然后根据对象渲染。</p>
</li>
</ul>
<h2 id="组合API（composition-api）"><a href="#组合API（composition-api）" class="headerlink" title="组合API（composition api）"></a>组合API（composition api）</h2><blockquote>
<p>vue2.x实现列表的增删功能</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">实现一个功能就需要</span></span><br><span class="line"><span class="comment">在data内定义数据，</span></span><br><span class="line"><span class="comment">在methods / computed / watch 中实现业务逻辑来操作数据</span></span><br><span class="line"><span class="comment">造成 数据和业务逻辑 分散</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;form&gt;</span><br><span class="line">      &lt;input placeholder=<span class="string">&quot;id&quot;</span> type=<span class="string">&quot;text&quot;</span>  v-model=<span class="string">&quot;info.id&quot;</span> /&gt;</span><br><span class="line">      &lt;br&gt;</span><br><span class="line">      &lt;input placeholder=<span class="string">&quot;name&quot;</span> type=<span class="string">&quot;text&quot;</span> v-model=<span class="string">&quot;info.name&quot;</span> /&gt;</span><br><span class="line">      &lt;br&gt;</span><br><span class="line">      &lt;input type=<span class="string">&quot;submit&quot;</span> @click=<span class="string">&quot;handleAdd&quot;</span> /&gt;</span><br><span class="line">    &lt;/form&gt;</span><br><span class="line">    &lt;ul&gt;</span><br><span class="line">      &lt;li</span><br><span class="line">        v-<span class="keyword">for</span>=<span class="string">&quot;(item,index) in list&quot;</span></span><br><span class="line">        :key=<span class="string">&quot;item.id&quot;</span></span><br><span class="line">        @click=<span class="string">&quot;handleRemove(index)&quot;</span></span><br><span class="line">      &gt;&#123;&#123;item.name&#125;&#125; ----- &#123;&#123; index &#125;&#125;&lt;/li&gt;</span><br><span class="line">    &lt;/ul&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line">  data() &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      info: &#123;</span><br><span class="line">        id:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        name:<span class="string">&#x27;&#x27;</span></span><br><span class="line">      &#125;,</span><br><span class="line">      list: [</span><br><span class="line">        &#123; <span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">&quot;jjj&quot;</span> &#125;,</span><br><span class="line">        &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">&quot;hhh&quot;</span> &#125;,</span><br><span class="line">        &#123; <span class="attr">id</span>: <span class="number">2</span>, <span class="attr">name</span>: <span class="string">&quot;0hh&quot;</span> &#125;,</span><br><span class="line">      ],</span><br><span class="line">      itemRefs: [],</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    handleRemove(index) &#123;</span><br><span class="line">      <span class="built_in">this</span>.list.splice(index, <span class="number">1</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    handleAdd(e)&#123;</span><br><span class="line">      e.preventDefault();</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="built_in">this</span>.info)</span><br><span class="line">      <span class="built_in">this</span>.list.push(<span class="built_in">this</span>.info)</span><br><span class="line">      <span class="built_in">this</span>.info = &#123;</span><br><span class="line">        id:<span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        name:<span class="string">&#x27;&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>vue3组合API的使用</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;h1&gt;&#123;&#123;count&#125;&#125;&lt;/h1&gt;</span><br><span class="line">    &lt;button @click=<span class="string">&quot;handleAdd&quot;</span>&gt;添加&lt;/button&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="comment">// 只能监听简单类型的变化，不能监听复杂类型的变化</span></span><br><span class="line"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 监听复杂类型的变化</span></span><br><span class="line"><span class="keyword">import</span> &#123; reactive &#125; form <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// setup为组合函数的入口函数</span></span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="comment">// 定义变量和初始化变量 count是一个对象，value是他的值</span></span><br><span class="line">    <span class="keyword">let</span> count = ref(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义方法</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleAdd</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      count.value ++</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 暴露变量和方法以供使用</span></span><br><span class="line">    <span class="keyword">return</span> &#123; count,handleAdd &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 可以将setup里面的内容单独抽离出来</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> &#123; obj, handleRemove &#125; = useRemove();</span><br><span class="line">    <span class="keyword">return</span> &#123; obj, handleRemove &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">useRemove</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> obj = reactive(&#123;</span><br><span class="line">    arr: [</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="string">&quot;1&quot;</span>, <span class="attr">name</span>: <span class="string">&quot;dorren&quot;</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="string">&quot;2&quot;</span>, <span class="attr">name</span>: <span class="string">&quot;jerry&quot;</span> &#125;,</span><br><span class="line">    ],</span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleRemove</span>(<span class="params">index</span>) </span>&#123;</span><br><span class="line">    obj.arr.splice(index, <span class="number">1</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; obj, handleRemove &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 使用的时候通过obj.arr使用</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 抽离方式2</span></span><br><span class="line"><span class="keyword">import</span> &#123; time, handleChange &#125; <span class="keyword">from</span> <span class="string">&quot;./hooks/useGetTime.ts&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123; time, handleChange &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ./hooks/useGetTime.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">let</span> time = ref(<span class="string">&quot;00:00:00&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> handleChange = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> date = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">    <span class="keyword">const</span> hour =</span><br><span class="line">        date.getHours() &lt; <span class="number">10</span> ? <span class="string">&quot;0&quot;</span> + date.getHours() : date.getHours();</span><br><span class="line">    <span class="keyword">const</span> min =</span><br><span class="line">        date.getMinutes() &lt; <span class="number">10</span> ? <span class="string">&quot;0&quot;</span> + date.getMinutes() : date.getMinutes();</span><br><span class="line">    <span class="keyword">const</span> sec =</span><br><span class="line">        date.getSeconds() &lt; <span class="number">10</span> ? <span class="string">&quot;0&quot;</span> + date.getSeconds() : date.getSeconds();</span><br><span class="line">    time.value = hour + <span class="string">&quot;:&quot;</span> + min + <span class="string">&quot;:&quot;</span> + sec;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        handleChange()</span><br><span class="line">    &#125;, <span class="number">1000</span>);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">export</span> &#123; time, handleChange &#125;</span><br></pre></td></tr></table></figure>

<p>组合API完整示例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;form&gt;</span><br><span class="line">      &lt;input placeholder=<span class="string">&quot;id&quot;</span> type=<span class="string">&quot;text&quot;</span> v-model=<span class="string">&quot;info.info.id&quot;</span> /&gt;</span><br><span class="line">      &lt;br /&gt;</span><br><span class="line">      &lt;input placeholder=<span class="string">&quot;name&quot;</span> type=<span class="string">&quot;text&quot;</span> v-model=<span class="string">&quot;info.info.name&quot;</span> /&gt;</span><br><span class="line">      &lt;br /&gt;</span><br><span class="line">      &lt;button @click=<span class="string">&quot;handleAdd&quot;</span>&gt;添加&lt;/button&gt;</span><br><span class="line">    &lt;/form&gt;</span><br><span class="line">    &lt;h1</span><br><span class="line">      v-<span class="keyword">for</span>=<span class="string">&quot;(item,index) in obj.arr&quot;</span></span><br><span class="line">      :key=<span class="string">&quot;item.id&quot;</span></span><br><span class="line">      @click=<span class="string">&quot;handleRemove(index)&quot;</span></span><br><span class="line">    &gt;&#123;&#123;item.name&#125;&#125;---&#123;&#123;item.id&#125;&#125;&lt;/h1&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">import</span> &#123; reactive &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> &#123; obj, handleRemove &#125; = useRemove();</span><br><span class="line">    <span class="comment">// 参数传递</span></span><br><span class="line">    <span class="keyword">let</span> &#123; info, handleAdd &#125; = useAdd(obj);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; obj, handleRemove, info, handleAdd &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">useRemove</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> obj = reactive(&#123;</span><br><span class="line">    arr: [</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="string">&quot;1&quot;</span>, <span class="attr">name</span>: <span class="string">&quot;dorren&quot;</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">id</span>: <span class="string">&quot;2&quot;</span>, <span class="attr">name</span>: <span class="string">&quot;jerry&quot;</span> &#125;,</span><br><span class="line">    ],</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleRemove</span>(<span class="params">index</span>) </span>&#123;</span><br><span class="line">    obj.arr.splice(index, <span class="number">1</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; obj, handleRemove &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">useAdd</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> info = reactive(&#123;</span><br><span class="line">    info: &#123;</span><br><span class="line">      id: <span class="string">&quot;&quot;</span>,</span><br><span class="line">      name: <span class="string">&quot;&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;);</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleAdd</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">    e.preventDefault();</span><br><span class="line">    <span class="keyword">const</span> item = <span class="built_in">Object</span>.assign(&#123;&#125;, info.info);</span><br><span class="line">    obj.arr.push(item);</span><br><span class="line">    info.info.id = <span class="string">&quot;&quot;</span>;</span><br><span class="line">    info.info.name = <span class="string">&quot;&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; info, handleAdd &#125;;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="组合API内使用生命周期钩子"><a href="#组合API内使用生命周期钩子" class="headerlink" title="组合API内使用生命周期钩子"></a>组合API内使用生命周期钩子</h3><table>
<thead>
<tr>
<th>选项API</th>
<th>Hook inside <code>setup</code></th>
</tr>
</thead>
<tbody><tr>
<td><code>beforeMount</code></td>
<td><code>onBeforeMount</code></td>
</tr>
<tr>
<td><code>mounted</code></td>
<td><code>onMounted</code></td>
</tr>
<tr>
<td><code>beforeUpdate</code></td>
<td><code>onBeforeUpdate</code></td>
</tr>
<tr>
<td><code>updated</code></td>
<td><code>onUpdated</code></td>
</tr>
<tr>
<td><code>beforeUnmount</code></td>
<td><code>onBeforeUnmount</code>vue2的beforeDestroy</td>
</tr>
<tr>
<td><code>unmounted</code></td>
<td><code>onUnmounted</code></td>
</tr>
<tr>
<td><code>errorCaptured</code></td>
<td><code>onErrorCaptured</code>捕获子组件异常</td>
</tr>
<tr>
<td><code>renderTracked</code></td>
<td><code>onRenderTracked</code>对变化数据进行跟踪</td>
</tr>
<tr>
<td><code>renderTriggered</code></td>
<td><code>onRenderTriggered</code>精确跟踪触发(有新旧值))</td>
</tr>
</tbody></table>
<h3 id="reactive"><a href="#reactive" class="headerlink" title="reactive"></a>reactive</h3><ul>
<li>vue3提供的实现响应式数据的方法  </li>
<li>用es6的proxy取代了defineProperty来实现响应式</li>
</ul>
<p>tips：</p>
<ul>
<li>reactive参数必须是对象（json/arr）</li>
<li>如果给reactive传递了其他类型<ul>
<li>默认情况下修改值，界面不会自动更新</li>
<li>如果想更新，可以通过重新赋值的方式</li>
</ul>
</li>
</ul>
<h3 id="ref"><a href="#ref" class="headerlink" title="ref"></a>ref</h3><ul>
<li>vue3提供的对简单值的监听</li>
<li>本质是reactive ref(xx) —&gt; reactive({ value: xx })</li>
</ul>
<p>tips:</p>
<ul>
<li>在vue中使用ref的值不用通过value获取 </li>
<li>在js中使用ref的值必须通过value获取  msg.value = 111</li>
</ul>
<h3 id="isRef和isReactive"><a href="#isRef和isReactive" class="headerlink" title="isRef和isReactive"></a>isRef和isReactive</h3><blockquote>
<p>用于判断变量类型</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; isRef,isReactive &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line">setup() &#123;</span><br><span class="line">  <span class="keyword">let</span> num = ref(<span class="number">99</span>)</span><br><span class="line">  <span class="built_in">console</span>.log( isRef(num) )  <span class="comment">// true</span></span><br><span class="line">  <span class="built_in">console</span>.log( isReactive(num) )  <span class="comment">// false</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="组件通信"><a href="#组件通信" class="headerlink" title="组件通信"></a>组件通信</h3><h4 id="父传子-props"><a href="#父传子-props" class="headerlink" title="父传子( props )"></a>父传子( props )</h4><blockquote>
<p>基本和vue2.x一样，父组件绑定属性，子组件通过props获取</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 父组件</span></span><br><span class="line">&lt;child-component :name=<span class="string">&quot;name&quot;</span> :age=<span class="string">&quot;count&quot;</span>  /&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 子组件</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span>&#123;</span><br><span class="line">  props:&#123;</span><br><span class="line">    name:&#123;</span><br><span class="line">      <span class="keyword">type</span>:<span class="built_in">String</span>,  <span class="comment">// 类型 String、Number、Boolean、Array、Object、Date、Function、Symbol</span></span><br><span class="line">      required:<span class="literal">true</span>,    <span class="comment">// 是否必传 true、false</span></span><br><span class="line">      <span class="keyword">default</span>:<span class="string">&#x27;jerry&#x27;</span>,  <span class="comment">// 默认值 对象或数组默认值必须从一个工厂函数获取 ()=&gt;...</span></span><br><span class="line"></span><br><span class="line">      <span class="comment">//  自定义验证函数 </span></span><br><span class="line">      validator:<span class="function">(<span class="params">value:<span class="built_in">string</span></span>)=&gt;</span> <span class="keyword">return</span> [<span class="string">&#x27;sherry&#x27;</span>,<span class="string">&#x27;jerry&#x27;</span>,<span class="string">&#x27;doreen&#x27;</span>].indexOf(value)!==<span class="number">-1</span></span><br><span class="line">    &#125;,</span><br><span class="line">    age:&#123;</span><br><span class="line">      <span class="comment">// ...</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 通过参数、在setup中使用</span></span><br><span class="line">  setup:<span class="function">(<span class="params">props</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="comment">// props是通过ref包裹的 响应式引用</span></span><br><span class="line">    <span class="built_in">console</span>.log(props.name)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="子传父-emit"><a href="#子传父-emit" class="headerlink" title="子传父( emit )"></a>子传父( emit )</h4><blockquote>
<p>有细微的差别</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 子组件</span></span><br><span class="line">&lt;button <span class="meta">@click</span>=<span class="string">&#x27;handleEmit&#x27;</span>&gt; 按钮 &lt;/button&gt;</span><br><span class="line"></span><br><span class="line">setup:<span class="function">(<span class="params">props,context</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="comment">// context 不是响应式的 可写为 &#123; attrs,slots,emit &#125; Attribute,插槽,广播</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleEmit</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    context.emit(<span class="string">&#x27;toParentComponent&#x27;</span>,<span class="string">&#x27;some info&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; handleEmit &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 父组件</span></span><br><span class="line">&lt;child-component <span class="meta">@toParentComponent</span>=<span class="string">&#x27;hanldeFun&#x27;</span> /&gt;</span><br><span class="line"></span><br><span class="line">setup:<span class="function">()=&gt;</span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">handleFun</span>(<span class="params">value:<span class="built_in">string</span></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value)  <span class="comment">// some info</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> &#123; handleFun &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="父传孙子-provide-inject"><a href="#父传孙子-provide-inject" class="headerlink" title="父传孙子( provide inject )"></a>父传孙子( provide inject )</h4><blockquote>
<p>父组件通过<code>provide</code>传递，孙子组件通过<code>inject</code>接收</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">// 父组件 传递</span></span><br><span class="line">setup: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  provide(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;value&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 子组件 接收</span></span><br><span class="line">setup: <span class="function">(<span class="params">props, context</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> result = inject(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h2 id="递归监听"><a href="#递归监听" class="headerlink" title="递归监听"></a>递归监听</h2><blockquote>
<p>无论是通过ref还是reactive创建的数据都是递归监听<br>能够监听每一层数据 并且把每一层数据都包装成proxy对象</p>
</blockquote>
<h2 id="非递归监听"><a href="#非递归监听" class="headerlink" title="非递归监听"></a>非递归监听</h2><blockquote>
<p><code>shallowReactive</code> 和 <code>shallowRef</code></p>
<ul>
<li>只为某个对象的私有（第一层）属性创建浅层的响应式代理，不会对“属性的属性”做深层次、递归地响应式代理，而只是保留原样。</li>
<li>只有第一层被封装成proxy对象，其余内容都是对象的形式存储，如果第一层内容没有被改变，就不会触发页面的更新</li>
</ul>
</blockquote>
<blockquote>
<p>Tips:<br>如果是通过<code>shallowRef</code>创建的数据，vue监听的是<code>.value</code>的变化，并不是第一层的变化。</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;date.data&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;date.obj.name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;date.obj.son.name&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;handleAdd&quot;</span>&gt;</span>添加<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; shallowReactive &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&quot;App&quot;</span>,</span><br><span class="line">  data() &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;&#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> date = shallowReactive(&#123;</span><br><span class="line">      date: <span class="string">&#x27;hhh&#x27;</span>,</span><br><span class="line">      obj: &#123;</span><br><span class="line">        name:<span class="string">&#x27;doreen&#x27;</span>,</span><br><span class="line">        son: &#123;</span><br><span class="line">          name: <span class="string">&#x27;jerry&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleAdd</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">// 第一层数据改变，点击事件后，页面数据更新</span></span><br><span class="line">      date.data = <span class="string">&#x27;eee&#x27;</span></span><br><span class="line">      date.obj.name = <span class="string">&#x27;sherry&#x27;</span></span><br><span class="line">      date.obj.son.name = <span class="string">&#x27;kris&#x27;</span></span><br><span class="line">      </span><br><span class="line">      <span class="comment">// 第一层数据没有改变，点击事件后，数据改变页面没有更新</span></span><br><span class="line">      date.obj.name = <span class="string">&#x27;sherry&#x27;</span></span><br><span class="line">      date.obj.son.name = <span class="string">&#x27;kris&#x27;</span></span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; date, handleAdd &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="triggerRef"><a href="#triggerRef" class="headerlink" title="triggerRef"></a>triggerRef</h2><blockquote>
<p>针对<code>ref</code>类型的数据，vue3提供了<code>triggerRef</code>的方法进行触发更新  </p>
</blockquote>
<p>（对于<code>reactive</code>类型的数据没有提供相应方法）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; triggerRef &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">date.value.son.name = <span class="string">&#x27;kris&#x27;</span></span><br><span class="line">triggerRef(date);</span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="toRow"><a href="#toRow" class="headerlink" title="toRow"></a>toRow</h2><blockquote>
<p>获取ref / reactive 的原始数据  </p>
</blockquote>
<p><code>ref</code> / <code>reactive</code> 数据类型的特点:<br>每次修改都会被追踪，触发界面更新，<code>但是会非常消耗性能</code><br>对于一些不需要追踪的操作，不需要更新界面，这个时候<br>就可以用<code>toRow</code>方法拿到他的原始数据，对原始数据进行修改<br>这样就不会被追踪并且不会更新界面，提高了性能。 </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; reactive,toRow &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> obj = &#123; <span class="attr">name</span>: <span class="string">&quot;doreen&quot;</span>,<span class="attr">sex</span>: <span class="string">&quot;女&quot;</span> &#125;;</span><br><span class="line">    <span class="keyword">let</span> state = reactive(obj);</span><br><span class="line">    <span class="keyword">let</span> obj2 = toRaw(state); <span class="comment">// 获取ref / reactive 的原始数据</span></span><br><span class="line">    <span class="comment">// let state = ref(obj);</span></span><br><span class="line">    <span class="comment">// let obj2 = toRow(state.value)  // ref需要通过.value获取原始数据</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// console.log(obj === obj2); // true</span></span><br><span class="line">    <span class="comment">// console.log( obj === state )    false</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    state 和 obj ：</span></span><br><span class="line"><span class="comment">    引用关系，state本质是一个proxy对象，在这个proxy对象中引用了obj</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">/*</span></span><br><span class="line"><span class="comment">      直接修改 obj 无法触发更新</span></span><br><span class="line"><span class="comment">      通过proxy对象包装后，才能触发页面更新</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">      obj.name = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      <span class="built_in">console</span>.log(obj);    <span class="comment">// &#123; name:&#x27;sherry&#x27;,sex:&#x27;女&#x27; &#125;</span></span><br><span class="line">      <span class="built_in">console</span>.log(state);  <span class="comment">// &#123; name:&#x27;sherry&#x27;,sex:&#x27;女&#x27; &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; obj, state, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="markRow"><a href="#markRow" class="headerlink" title="markRow"></a>markRow</h2><blockquote>
<p>让数据永远不会被追踪</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> obj = markRaw(&#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">      sex: <span class="string">&quot;女&quot;</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">let</span> state = ref(obj);</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">// 不触发页面更新</span></span><br><span class="line">      obj.name = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      <span class="built_in">console</span>.log(obj); <span class="comment">// &#123; name:&#x27;sherry&#x27;,sex:&#x27;女&#x27; &#125;</span></span><br><span class="line">      <span class="built_in">console</span>.log(state); <span class="comment">// &#123; name:&#x27;sherry&#x27;,sex:&#x27;女&#x27; &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; state, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>

<h2 id="toRef"><a href="#toRef" class="headerlink" title="toRef"></a>toRef</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> obj = &#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * let state = ref(obj.name)</span></span><br><span class="line"><span class="comment">     * =&gt; let state = ref(&#x27;doreen&#x27;)</span></span><br><span class="line"><span class="comment">     * =&gt; let state = reactive(&#123; value: &#x27;doreen&#x27; &#125;)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">let</span> state = ref(obj.name);</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      state.value = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * 如果利用ref将某一个对象中的属性变成响应式的数据</span></span><br><span class="line"><span class="comment">       * 我们修改的响应式数据不会影响到原始数据</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">      <span class="built_in">console</span>.log(obj); <span class="comment">// &#123; name:&#x27;doreen&#x27; &#125;</span></span><br><span class="line">      <span class="built_in">console</span>.log(state); <span class="comment">// &#123; value: &#x27;sherry&#x27; &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; state, handleChange &#125;;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> obj = &#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    ref和toRef的区别：</span></span><br><span class="line"><span class="comment">    ref -&gt; 复制，修改响应式数据不会影响以前的数据</span></span><br><span class="line"><span class="comment">           数据发生改变，界面就会自动更新</span></span><br><span class="line"><span class="comment">    </span></span><br><span class="line"><span class="comment">    toRef -&gt; 引用，修改响应式数据会影响以前的数据</span></span><br><span class="line"><span class="comment">             数据发生改变，界面也不会自动更新</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="keyword">let</span> state = toRef(obj,<span class="string">&#x27;name&#x27;</span>);</span><br><span class="line">    <span class="built_in">console</span>.log(state)</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      state.value = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * 如果利用toRef将某一个对象中的属性变成响应式的数据</span></span><br><span class="line"><span class="comment">       * 我们修改的响应式数据会影响到原始数据</span></span><br><span class="line"><span class="comment">       * 如果响应式的数据通过toRef创建的，修改数据并不会触发界面更新</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">      <span class="built_in">console</span>.log(obj); <span class="comment">// &#123; name:&#x27;sherry&#x27; &#125;</span></span><br><span class="line">      <span class="built_in">console</span>.log(state); <span class="comment">// &#123; value: &#x27;sherry&#x27; &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123; state, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>

<h2 id="customRef"><a href="#customRef" class="headerlink" title="customRef"></a>customRef</h2><blockquote>
<p>返回一个ref对象，可以显式的控制依赖追踪和触发响应</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; customRef &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 自定义 ref</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myRef</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// track -&gt; 追踪  trigger -&gt; 触发</span></span><br><span class="line">  <span class="comment">// 没有这两个参数，ui界面就不会被触发更新</span></span><br><span class="line">  <span class="keyword">return</span> customRef(<span class="function">(<span class="params">track,trigger</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      get() &#123;</span><br><span class="line">        track() <span class="comment">// 告诉vue 数据需要被追踪变化的 </span></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;get:&quot;</span>, value);</span><br><span class="line">        <span class="keyword">return</span> value;</span><br><span class="line">      &#125;,</span><br><span class="line">      set(newV) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;set:&quot;</span>, newV);</span><br><span class="line">        value = newV</span><br><span class="line">        trigger() <span class="comment">// 触发更新</span></span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> state = myRef(<span class="number">15</span>);</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      state.value++;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123; state, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>应用场景：<br>setup中不能进行异步操作<br>如果需要通过网络请求对数据进行赋值<br>就可以使用<code>customRef</code>来获取请求数据</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">import</span> &#123; customRef &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">myRef</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// track -&gt; 追踪  trigger -&gt; 触发</span></span><br><span class="line">  <span class="keyword">return</span> customRef(<span class="function">(<span class="params">track, trigger</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 在get之前对数据进行赋值 不会造成重复刷新</span></span><br><span class="line">    fetch(value)</span><br><span class="line">      .then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">JSON</span>.json();</span><br><span class="line">      &#125;)</span><br><span class="line">      .then(<span class="function">(<span class="params">data</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(data);</span><br><span class="line">        value = data;</span><br><span class="line">        trigger(); <span class="comment">// 获取数据、刷新页面</span></span><br><span class="line">      &#125;)</span><br><span class="line">      .catch(<span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(err);</span><br><span class="line">      &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      get() &#123;</span><br><span class="line">        track(); <span class="comment">// 数据需要被追踪变化的</span></span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;get:&quot;</span>, value);</span><br><span class="line">        <span class="keyword">return</span> value;</span><br><span class="line">      &#125;,</span><br><span class="line">      set(newV) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;set:&quot;</span>, newV);</span><br><span class="line">        value = newV;</span><br><span class="line">        trigger(); <span class="comment">// 触发更新</span></span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> state = myRef(<span class="string">&#x27;../public/data.json&#x27;</span>);</span><br><span class="line">    <span class="keyword">return</span> &#123; state &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;; </span><br></pre></td></tr></table></figure>

<h2 id="获取元素"><a href="#获取元素" class="headerlink" title="获取元素"></a>获取元素</h2><blockquote>
<p>vue2x中通过绑定ref属性，然后通过$refs.xxx来获取元素<br>vue3取消了$refs,但是也可以通过ref来获取元素</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;h1 ref=<span class="string">&quot;box&quot;</span>&gt;&lt;/h1&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; ref, onMounted &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> box = ref(<span class="literal">null</span>);</span><br><span class="line">    </span><br><span class="line">    onMounted(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(box.value); <span class="comment">// &lt;h1&gt;&lt;/h1&gt;</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">// setup的生命周期在beforeCreated之前 </span></span><br><span class="line">    <span class="built_in">console</span>.log(box.value) <span class="comment">// null</span></span><br><span class="line">    <span class="keyword">return</span> &#123; box &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


<h2 id="readonly"><a href="#readonly" class="headerlink" title="readonly"></a>readonly</h2><blockquote>
<p>用于创建一个只读的数据，并且递归只读</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; readonly &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> box = readonly(&#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">      attr: &#123;</span><br><span class="line">        age: <span class="number">12</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      box.name = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      box.attr.age = <span class="number">22</span>;</span><br><span class="line"></span><br><span class="line">      <span class="built_in">console</span>.log(box);</span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * &#123;</span></span><br><span class="line"><span class="comment">          name: &quot;doreen&quot;,</span></span><br><span class="line"><span class="comment">          attr: &#123;</span></span><br><span class="line"><span class="comment">              age: 12,</span></span><br><span class="line"><span class="comment">          &#125;,</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123; box, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="shallowReadonly"><a href="#shallowReadonly" class="headerlink" title="shallowReadonly"></a>shallowReadonly</h2><blockquote>
<p>创建一个<code>第一层</code>只读的数据 (非递归)</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; shallowReadonly &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">let</span> box = shallowReadonly(&#123;</span><br><span class="line">      name: <span class="string">&quot;doreen&quot;</span>,</span><br><span class="line">      attr: &#123;</span><br><span class="line">        age: <span class="number">12</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">handleChange</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      box.name = <span class="string">&quot;sherry&quot;</span>;</span><br><span class="line">      box.attr.age = <span class="number">22</span>;</span><br><span class="line">      </span><br><span class="line">      <span class="built_in">console</span>.log(box);</span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * &#123;</span></span><br><span class="line"><span class="comment">          name: &quot;doreen&quot;,</span></span><br><span class="line"><span class="comment">          attr: &#123;</span></span><br><span class="line"><span class="comment">              age: 22,</span></span><br><span class="line"><span class="comment">          &#125;,</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123; box, handleChange &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="isReadonly"><a href="#isReadonly" class="headerlink" title="isReadonly"></a>isReadonly</h2><blockquote>
<p>判断是否是<code>readonly</code>类型 返回布尔值</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">isReadonly(state) <span class="comment">// true / false</span></span><br></pre></td></tr></table></figure>

<h2 id="computed"><a href="#computed" class="headerlink" title="computed"></a>computed</h2><blockquote>
<p>使用getter函数，从getter返回的值返回一个不变的响应式<code>ref</code>对象</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">const</span> count = ref(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">const</span> plusOne = computed(<span class="function">() =&gt;</span> count.value + <span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(plusOne.value) <span class="comment">// 2</span></span><br><span class="line">plusOne.value++ <span class="comment">// error</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>通过<code>get</code>和<code>set</code>函数对象创建ref对象</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">let</span> msg = ref(<span class="string">&quot;title&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> str = computed(&#123;</span><br><span class="line">  get: <span class="function">() =&gt;</span> msg.value + <span class="string">&quot; get &quot;</span>,</span><br><span class="line">  set: <span class="function">(<span class="params">val</span>) =&gt;</span> &#123;</span><br><span class="line">    msg.value = val + <span class="string">&quot; set &quot;</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(str.value);   <span class="comment">// title get</span></span><br><span class="line">str.value = <span class="string">&quot;set value&quot;</span>;  <span class="comment">// set value</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="watch"><a href="#watch" class="headerlink" title="watch"></a>watch</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; watch &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ...</span></span><br><span class="line">    <span class="comment">// 多个观察对象 watch([val1,val2 ... ],([])=&gt;&#123; ... &#125;)</span></span><br><span class="line">    watch(msg, <span class="function">(<span class="params">newV, oldV</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(newV, oldV);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// ...</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Teleport组件"><a href="#Teleport组件" class="headerlink" title="Teleport组件"></a>Teleport组件</h2><blockquote>
<p>vue项目在编译后所有的内容都是在 <code>&lt;div id=&#39;app&#39;&gt;&lt;/div&gt;</code> 标签下进行的，样式什么的会受到全局样式的影响</p>
</blockquote>
<blockquote>
<p><code>Teleport</code>是一种能够将我们的模板移动到 DOM 中 Vue app 之外的其他位置的技术</p>
</blockquote>
<h3 id="属性"><a href="#属性" class="headerlink" title="属性"></a>属性</h3><h4 id="to"><a href="#to" class="headerlink" title="to"></a>to</h4><blockquote>
<p><code>&lt;teleport to=&quot;#some-id / .some-class / [data-teleport]&quot; /&gt;</code> to属性后面必须是有效的查询选择器或 HTMLElement (如果在浏览器环境中使用)。目的是将<code>teleport</code>内的元素移动到该容器中</p>
</blockquote>
<h4 id="disabled"><a href="#disabled" class="headerlink" title="disabled"></a>disabled</h4><blockquote>
<p><code>&lt;teleport to=&#39;...&#39; :disabled=&#39;disabledFlag&#39;&gt;</code>此可选属性可用于禁用 <code>&lt;teleport&gt;</code> 的功能，这意味着其插槽内容将不会移动到任何位置，而是在您在周围父组件中指定了 <code>&lt;teleport&gt;</code> 的位置渲染</p>
</blockquote>
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><ol>
<li><p>public - index.html<br>在 <code>&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;</code> 下方或者什么地方新增 <code>&lt;div id=&quot;example&quot;&gt;&lt;/div&gt;</code></p>
</li>
<li><p>新建一个 Example.vue 文件 并挂载到 App.vue 中进行展示</p>
</li>
</ol>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Example.vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">teleport</span> <span class="attr">to</span>=<span class="string">&#x27;#example&#x27;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span> ... <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">teleport</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="css">  <span class="comment">/* ... */</span></span></span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- App.vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    ....</span><br><span class="line">    <span class="tag">&lt;<span class="name">example</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>这样Example.vue所写的内容就存在于<code>&lt;div id=&quot;example&quot;&gt;&lt;/div&gt;</code>标签中，而不是<code>&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;</code>中，样式不会受app的影响</p>
<h2 id="Suspense异步组件"><a href="#Suspense异步组件" class="headerlink" title="Suspense异步组件"></a>Suspense异步组件</h2><blockquote>
<p>Suspense组件用于在等待某个异步组件解析时显示后备内容</p>
</blockquote>
<h3 id="使用-1"><a href="#使用-1" class="headerlink" title="使用"></a>使用</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> <span class="comment">&lt;!-- Example.vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span> &#123;&#123;result&#125;&#125; <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&#x27;ts&#x27;</span>&gt;</span></span><br><span class="line">...</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> defineComponent(&#123;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">async</span> setup()&#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> rawData = <span class="keyword">await</span> axios.get(<span class="string">&#x27;...&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123; <span class="attr">result</span>:rowData.data &#125;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="css">  <span class="comment">/* ... */</span></span></span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">&lt;!-- App.vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">Suspense</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> #<span class="attr">default</span>&gt;</span></span><br><span class="line">      <span class="comment">&lt;!-- 请求成功后展示的组件 --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">example</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> #<span class="attr">fallback</span>&gt;</span></span><br><span class="line">      <span class="comment">&lt;!-- 没请求的时候展示的组件 --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Loading...<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">Suspense</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&#x27;ts&#x27;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="comment">// 捕获请求异常</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; onErrorCaptured &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span>&#123;</span></span><br><span class="line">  ...</span><br><span class="line">  setup()&#123;</span><br><span class="line"><span class="javascript">    <span class="comment">// 返回bool值</span></span></span><br><span class="line"><span class="javascript">    onErrorCaptured(<span class="function">(<span class="params">error</span>)=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">console</span>.log(error)</span></span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> <span class="literal">true</span></span></span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-vue3原生实现"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/10/05/vue3%E5%8E%9F%E7%94%9F%E5%AE%9E%E7%8E%B0/"
    >vue3响应式的实现</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/10/05/vue3%E5%8E%9F%E7%94%9F%E5%AE%9E%E7%8E%B0/" class="article-date">
  <time datetime="2020-10-05T06:17:00.000Z" itemprop="datePublished">2020-10-05</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/vue/">vue</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <blockquote>
<p>非递归监听，只有第一层改变才会触发界面更新</p>
</blockquote>
<h2 id="shallowReactive"><a href="#shallowReactive" class="headerlink" title="shallowReactive"></a>shallowReactive</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">shallowReactive</span>(<span class="params">obj</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Proxy</span>(obj,&#123;</span><br><span class="line">        get(obj,key)&#123;</span><br><span class="line">            <span class="keyword">return</span> obj[key]</span><br><span class="line">        &#125;,</span><br><span class="line">        set(obj,key,value)&#123;</span><br><span class="line">            obj[key] = value</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;<span class="attr">name</span>:<span class="string">&#x27;doreen&#x27;</span>&#125;</span><br><span class="line"><span class="keyword">var</span> state = <span class="keyword">new</span> shallowReactive(obj)</span><br><span class="line">state.name = <span class="string">&#x27;sherry&#x27;</span> <span class="comment">// &#123; name: &#x27;sherry&#x27; &#125;</span></span><br></pre></td></tr></table></figure>


<h2 id="shallowRef"><a href="#shallowRef" class="headerlink" title="shallowRef"></a>shallowRef</h2><blockquote>
<p>在shallowReactive的基础上，在外部封装一层value，所以他的第一层是value，只有value改变才会触发界面更新</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">shallowRef</span>(<span class="params">val</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> shallowReactive(&#123;<span class="attr">value</span>: val&#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">shallowReactive</span>(<span class="params">obj</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Proxy</span>(obj,&#123;</span><br><span class="line">        get(obj,key)&#123;</span><br><span class="line">            <span class="keyword">return</span> obj[key]</span><br><span class="line">        &#125;,</span><br><span class="line">        set(obj,key,value)&#123;</span><br><span class="line">            obj[key] = value</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> obj = &#123;<span class="attr">name</span>:<span class="string">&#x27;doreen&#x27;</span>&#125;</span><br><span class="line"><span class="keyword">var</span> state = <span class="keyword">new</span> shallowRef(obj)</span><br><span class="line">state.value.name = <span class="string">&#x27;haha&#x27;</span> <span class="comment">// &#123; value: &#123; name: &#x27;haha&#x27; &#125; &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="shallowReadonly"><a href="#shallowReadonly" class="headerlink" title="shallowReadonly"></a>shallowReadonly</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">shallowReadonly</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Proxy</span>(obj, &#123;</span><br><span class="line">        get(obj, key) &#123;</span><br><span class="line">            <span class="keyword">return</span> obj[key]</span><br><span class="line">        &#125;,</span><br><span class="line">        set(obj, key, value) &#123;</span><br><span class="line">            <span class="built_in">console</span>.warn(<span class="string">&#x27;只读 不能被赋值&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<blockquote>
<p>非递归监听，每一层改变都会触发更新。原理是把每一层进行proxy封装</p>
</blockquote>
<h2 id="reactive"><a href="#reactive" class="headerlink" title="reactive"></a>reactive</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">reactive</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> obj === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (obj <span class="keyword">instanceof</span> <span class="built_in">Array</span>) &#123;</span><br><span class="line">            obj.forEach(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="keyword">typeof</span> item === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">                    obj[index] = reactive(item)</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> obj) &#123;</span><br><span class="line">                <span class="keyword">let</span> item = obj[key];</span><br><span class="line">                <span class="keyword">if</span> (<span class="keyword">typeof</span> item === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">                    obj[key] = reactive(item)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.warn(<span class="string">`<span class="subst">$&#123;obj&#125;</span> is not object`</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Proxy</span>(obj, &#123;</span><br><span class="line">        get(obj, key) &#123;</span><br><span class="line">            <span class="keyword">return</span> obj[key]</span><br><span class="line">        &#125;,</span><br><span class="line">        set(obj, key, value) &#123;</span><br><span class="line">            obj[key] = value</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="ref"><a href="#ref" class="headerlink" title="ref"></a>ref</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ref</span>(<span class="params">val</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> reactive(&#123; <span class="attr">value</span>: val &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">reactive</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> obj === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (obj <span class="keyword">instanceof</span> <span class="built_in">Array</span>) &#123;</span><br><span class="line">            obj.forEach(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="keyword">typeof</span> item === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">                    obj[index] = reactive(item)</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> obj) &#123;</span><br><span class="line">                <span class="keyword">let</span> item = obj[key];</span><br><span class="line">                <span class="keyword">if</span> (<span class="keyword">typeof</span> item === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">                    obj[key] = reactive(item)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.warn(<span class="string">`<span class="subst">$&#123;obj&#125;</span> is not object`</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Proxy</span>(obj, &#123;</span><br><span class="line">        get(obj, key) &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(obj, key)</span><br><span class="line">            <span class="keyword">return</span> obj[key]</span><br><span class="line">        &#125;,</span><br><span class="line">        set(obj, key, value) &#123;</span><br><span class="line">            obj[key] = value</span><br><span class="line">            <span class="built_in">console</span>.log(obj)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="readonly"><a href="#readonly" class="headerlink" title="readonly"></a>readonly</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">readonly</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> obj === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (obj <span class="keyword">instanceof</span> <span class="built_in">Array</span>) &#123;</span><br><span class="line">            obj.forEach(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="keyword">typeof</span> item === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">                    obj[index] = readonly(item)</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> obj) &#123;</span><br><span class="line">                <span class="keyword">let</span> item = obj[key];</span><br><span class="line">                <span class="keyword">if</span> (<span class="keyword">typeof</span> item === <span class="string">&#x27;object&#x27;</span>) &#123;</span><br><span class="line">                    obj[key] = readonly(item)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.warn(<span class="string">`<span class="subst">$&#123;obj&#125;</span> is not object`</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Proxy</span>(obj, &#123;</span><br><span class="line">        get(obj, key) &#123;</span><br><span class="line">            <span class="keyword">return</span> obj[key]</span><br><span class="line">        &#125;,</span><br><span class="line">        set(obj, key, value) &#123;</span><br><span class="line">            <span class="built_in">console</span>.warn(<span class="string">&#x27;不能被赋值&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-vue中的key属性"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/10/01/vue%E4%B8%AD%E7%9A%84key%E5%B1%9E%E6%80%A7/"
    >vue的key属性</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/10/01/vue%E4%B8%AD%E7%9A%84key%E5%B1%9E%E6%80%A7/" class="article-date">
  <time datetime="2020-10-01T03:37:33.627Z" itemprop="datePublished">2020-10-01</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/vue/">vue</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="前提"><a href="#前提" class="headerlink" title="前提"></a>前提</h2><blockquote>
<p>在form表单需要实现两个输入框,绑定对象的两个属性一个存储key，一个存储value</p>
</blockquote>
<h2 id="遇到的问题"><a href="#遇到的问题" class="headerlink" title="遇到的问题"></a>遇到的问题</h2><blockquote>
<p>绑定值（key）的输入框，在一次输入后锚点就消失了，绑定名称（name）的输入框可以正常的使用</p>
</blockquote>
<h2 id="错误代码"><a href="#错误代码" class="headerlink" title="错误代码"></a>错误代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">statusType = [&#123;</span><br><span class="line">    key:<span class="string">&quot;&quot;</span>,</span><br><span class="line">    value:<span class="string">&quot;&quot;</span></span><br><span class="line">&#125;]</span><br><span class="line"></span><br><span class="line">&lt;el-form-item</span><br><span class="line">    prop=<span class="string">&quot;valueRange&quot;</span></span><br><span class="line">    v-<span class="keyword">for</span>=<span class="string">&quot;item in statusType&quot;</span></span><br><span class="line">    :key=<span class="string">&quot;item.key&quot;</span></span><br><span class="line">    :value=<span class="string">&quot;item.key&quot;</span></span><br><span class="line">&gt;</span><br><span class="line">    &lt;el-col :span=<span class="string">&quot;10&quot;</span>&gt;</span><br><span class="line">        &lt;el-input placeholder=<span class="string">&quot;值&quot;</span> v-model=<span class="string">&quot;item.key&quot;</span>&gt;&lt;/el-input&gt;</span><br><span class="line">    &lt;/el-col&gt;</span><br><span class="line">    &lt;el-col :span=<span class="string">&quot;4&quot;</span> style=<span class="string">&quot;text-align:center&quot;</span>&gt;:&lt;/el-col&gt;</span><br><span class="line">    &lt;el-col :span=<span class="string">&quot;10&quot;</span>&gt;</span><br><span class="line">        &lt;el-input placeholder=<span class="string">&quot;名称&quot;</span> v-model=<span class="string">&quot;item.value&quot;</span>&gt;&lt;/el-input&gt;</span><br><span class="line">    &lt;/el-col&gt;</span><br><span class="line">&lt;/el-form-item&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="原因"><a href="#原因" class="headerlink" title="原因"></a>原因</h2><pre>
在列表循环的时候key绑定的是item.key  // el-form-item :key="item.key" 
但是 值 输入框中绑定的数据也是 item.key
因为Vue是基于虚拟dom对元素进行重新渲染
每次输入都会改变key值，key属性值的改变会导致dom的重新渲染
所以锚点会消失！
</pre>

<h2 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h2><blockquote>
<p>将父级元素的key属性通过index来绑定</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;el-form-item</span><br><span class="line">    prop=<span class="string">&quot;valueRange&quot;</span></span><br><span class="line">    v-<span class="keyword">for</span>=<span class="string">&quot;(item,index) in statusType&quot;</span></span><br><span class="line">    :key=<span class="string">&quot;index&quot;</span></span><br><span class="line">    :value=<span class="string">&quot;item.key&quot;</span></span><br><span class="line">&gt;</span><br><span class="line">    &lt;el-col :span=<span class="string">&quot;10&quot;</span>&gt;</span><br><span class="line">        &lt;el-input placeholder=<span class="string">&quot;值&quot;</span> v-model=<span class="string">&quot;item.key&quot;</span>&gt;&lt;/el-input&gt;</span><br><span class="line">    &lt;/el-col&gt;</span><br><span class="line">    &lt;el-col :span=<span class="string">&quot;4&quot;</span> style=<span class="string">&quot;text-align:center&quot;</span>&gt;:&lt;/el-col&gt;</span><br><span class="line">    &lt;el-col :span=<span class="string">&quot;10&quot;</span>&gt;</span><br><span class="line">        &lt;el-input placeholder=<span class="string">&quot;名称&quot;</span> v-model=<span class="string">&quot;item.value&quot;</span>&gt;&lt;/el-input&gt;</span><br><span class="line">    &lt;/el-col&gt;</span><br><span class="line">&lt;/el-form-item&gt;</span><br></pre></td></tr></table></figure>
<h2 id="扩展应用"><a href="#扩展应用" class="headerlink" title="扩展应用"></a>扩展应用</h2><h3 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h3><blockquote>
<p>页面有上下两个表格，上 -&gt; 订单表、下 -&gt; 详细表<br>通过点击订单表的某一行订单，获取到他的<code>inboundOrderSn</code>字段，并通过该字段向接口发送请求，获取详细数据并在下方的表格显示 </p>
</blockquote>
<h3 id="难点"><a href="#难点" class="headerlink" title="难点"></a>难点</h3><blockquote>
<p>子组件封装的是在刷新之后对接口进行请求等价于在mounted/created内才会请求数据，但是组件在页面一开始就已经创建完成了，所以在点击的时候并不会进行数据的请求，就得需要重新渲染该组件。</p>
</blockquote>
<h3 id="解决方法"><a href="#解决方法" class="headerlink" title="解决方法"></a>解决方法</h3><blockquote>
<p>给详细列表组件绑定key属性，在点击某一订单的时候，对key值进行改变，组件就会重新渲染一次。就可以发送请求并对数据进行展示</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;detail-table :key=<span class="string">&quot;timer&quot;</span> :id=<span class="string">&quot;selectedId&quot;</span> @success=<span class="string">&quot;forceRefresh&quot;</span>&gt;&lt;/detail-table&gt;</span><br><span class="line"></span><br><span class="line">private timer: number = <span class="keyword">new</span> <span class="built_in">Date</span>().getTime();</span><br><span class="line"></span><br><span class="line">private handleRowClick(row: any) &#123;</span><br><span class="line">    <span class="built_in">this</span>.timer = <span class="keyword">new</span> <span class="built_in">Date</span>().getTime();</span><br><span class="line">    <span class="built_in">this</span>.selectedId = row.inboundOrderSn ? row.inboundOrderSn : <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-vuex"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/10/01/vuex/"
    >vuex简单使用</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/10/01/vuex/" class="article-date">
  <time datetime="2020-10-01T03:37:33.623Z" itemprop="datePublished">2020-10-01</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/vue/">vue</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><blockquote>
<p>npm i vuex -S</p>
</blockquote>
<h2 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h2><h3 id="在src下创建一个store文件夹文件夹内创建index-js文件"><a href="#在src下创建一个store文件夹文件夹内创建index-js文件" class="headerlink" title="在src下创建一个store文件夹文件夹内创建index.js文件"></a>在src下创建一个store文件夹文件夹内创建index.js文件</h3><h3 id="配置（index-js）文件"><a href="#配置（index-js）文件" class="headerlink" title="配置（index.js）文件"></a>配置（index.js）文件</h3><ol>
<li><p>引入组件vuex</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>定义数据</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 数据存储中心 对应于vue中的data</span></span><br><span class="line"><span class="keyword">const</span> state = &#123;</span><br><span class="line">    count: <span class="number">0</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 数据操作中心 操作state内的数据的变化</span></span><br><span class="line"><span class="comment">// 只提供同步操作</span></span><br><span class="line"><span class="keyword">const</span> mutations =&#123;</span><br><span class="line">    addCount() &#123;</span><br><span class="line">        state.count += <span class="number">1</span></span><br><span class="line">    &#125;,</span><br><span class="line">    deCount() &#123;</span><br><span class="line">        state.count -= <span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 类似于mutation，他提交的是一个mutation而不是直接改变state内数据的状态</span></span><br><span class="line"><span class="comment">// 可以包含任意异步操作</span></span><br><span class="line"><span class="keyword">const</span> actions = &#123;</span><br><span class="line">    changeCount(context)&#123;</span><br><span class="line">        context.commit(<span class="string">&#x27;addCount&#x27;</span>)  <span class="comment">// addCount是mutations的一个方法名</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 对state进行计算操作，类似于vue中的计算属性</span></span><br><span class="line"><span class="comment">// 适用于多个组件复用的计算属性</span></span><br><span class="line"><span class="keyword">const</span> getters = &#123;</span><br><span class="line">    computedCount: <span class="function">(<span class="params">state</span>)=&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> state.count*<span class="number">2</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>实例化Vuex并暴露出去</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    state,</span><br><span class="line">    mutations,</span><br><span class="line">    getters,</span><br><span class="line">    actions</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store</span><br></pre></td></tr></table></figure>
<h3 id="在main-js中引入使用store"><a href="#在main-js中引入使用store" class="headerlink" title="在main.js中引入使用store"></a>在main.js中引入使用store</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引入store</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;./store&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  <span class="comment">// 在vue实例化中使用store</span></span><br><span class="line">  store,</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h2></li>
</ol>
<ul>
<li>state 通过 this.$store.state.xxx 来使用</li>
<li>mutation 通过 this.$store.commit(‘xxx’) 来使用</li>
<li>getter 通过 this.$store.getters.xxx 来使用</li>
<li>action 通过 this.$store.dispatch(‘xxx’) 来使用</li>
</ul>
<h2 id="配合辅助函数的使用"><a href="#配合辅助函数的使用" class="headerlink" title="配合辅助函数的使用"></a>配合辅助函数的使用</h2><h3 id="MapState："><a href="#MapState：" class="headerlink" title="MapState："></a>MapState：</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;mapState&#125; <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span></span><br><span class="line">…</span><br><span class="line">computed: &#123;</span><br><span class="line">    ...mapState([</span><br><span class="line"> 			  <span class="comment">/**</span></span><br><span class="line"><span class="comment">             *  当映射的计算属性的名称与state的子节点名称相同</span></span><br><span class="line"><span class="comment">             * 我们可以直接给mapState传一个字符串数组</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="string">&#x27;name&#x27;</span>,  <span class="comment">//映射 this.name 为 store.state.name</span></span><br><span class="line">            <span class="string">&#x27;age&#x27;</span></span><br><span class="line">        ])</span><br><span class="line"></span><br><span class="line">    ...mapState(&#123;</span><br><span class="line">            vName:  <span class="function"><span class="params">state</span>=&gt;</span>state.name,  <span class="comment">//映射 this.vName 为 store.state.name</span></span><br><span class="line">            vAge:  <span class="function"><span class="params">state</span> =&gt;</span>state.age</span><br><span class="line">        &#125;)</span><br><span class="line"></span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></figure>

<h3 id="mapGetters："><a href="#mapGetters：" class="headerlink" title="mapGetters："></a>mapGetters：</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">computed: &#123;</span><br><span class="line">        ...mapGetters(&#123;</span><br><span class="line">            name: <span class="string">&#x27;NAME&#x27;</span>    <span class="comment">//异名映射</span></span><br><span class="line">        &#125;),</span><br><span class="line">        ...mapGetters([</span><br><span class="line">            <span class="string">&#x27;NAME&#x27;</span>      <span class="comment">//同名映射</span></span><br><span class="line">        ])</span><br><span class="line">    &#125;,</span><br></pre></td></tr></table></figure>

<h3 id="mapMutations："><a href="#mapMutations：" class="headerlink" title="mapMutations："></a>mapMutations：</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">methods: &#123;</span><br><span class="line">    ...mapMutations([</span><br><span class="line">      <span class="string">&quot;increment&quot;</span>, <span class="comment">// 将 `this.increment()` 映射为 `this.$store.commit(&#x27;increment&#x27;)`</span></span><br><span class="line">]),</span><br><span class="line">    ...mapMutations(&#123;</span><br><span class="line">      add: <span class="string">&quot;increment&quot;</span> <span class="comment">// 将 `this.add()` 映射为 `this.$store.commit(&#x27;increment&#x27;)`</span></span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<h3 id="mapAction："><a href="#mapAction：" class="headerlink" title="mapAction："></a>mapAction：</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">methods: &#123;</span><br><span class="line">    ...mapActions([</span><br><span class="line">      <span class="string">&#x27;increment&#x27;</span>, <span class="comment">// 将 `this.increment()` 映射为 `this.$store.dispatch(&#x27;increment&#x27;)`</span></span><br><span class="line"></span><br><span class="line">      <span class="comment">// `mapActions` 也支持载荷：</span></span><br><span class="line">      <span class="string">&#x27;incrementBy&#x27;</span> <span class="comment">// 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch(&#x27;incrementBy&#x27;, amount)`</span></span><br><span class="line">    ]),</span><br><span class="line">    ...mapActions(&#123;</span><br><span class="line">      add: <span class="string">&#x27;increment&#x27;</span> <span class="comment">// 将 `this.add()` 映射为 `this.$store.dispatch(&#x27;increment&#x27;)`</span></span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-vue-router"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/10/01/vue-router/"
    >vue-router的简单使用</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/10/01/vue-router/" class="article-date">
  <time datetime="2020-10-01T03:37:33.622Z" itemprop="datePublished">2020-10-01</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/vue/">vue</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><blockquote>
<p>npm i vue-router -S</p>
</blockquote>
<h2 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h2><h3 id="在src下创建一个router文件夹文件夹内创建index-js文件"><a href="#在src下创建一个router文件夹文件夹内创建index-js文件" class="headerlink" title="在src下创建一个router文件夹文件夹内创建index.js文件"></a>在src下创建一个router文件夹文件夹内创建index.js文件</h3><h3 id="配置路由（index-js）文件"><a href="#配置路由（index-js）文件" class="headerlink" title="配置路由（index.js）文件"></a>配置路由（index.js）文件</h3><ol>
<li><p>创建组件、引入组件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Router <span class="keyword">from</span> <span class="string">&#x27;vue-router&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Nav <span class="keyword">from</span> <span class="string">&#x27;../components/Nav.vue&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>配置路由</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line">    &#123; <span class="attr">path</span>:<span class="string">&#x27;/nav&#x27;</span>,<span class="attr">component</span>:Nav &#125;, </span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">&#x27;/user/:id&#x27;</span>, <span class="attr">component</span>: User &#125; <span class="comment">// 动态路由</span></span><br><span class="line">    <span class="comment">// ....</span></span><br><span class="line">    &#123; <span class="attr">path</span>:<span class="string">&#x27;*&#x27;</span>,<span class="attr">redirect</span>:<span class="string">&#x27;/nav&#x27;</span> &#125;,   <span class="comment">// 默认跳转路径</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 嵌套路由</span></span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">&#x27;/user/:id&#x27;</span>, <span class="attr">component</span>: User,</span><br><span class="line">      children: [</span><br><span class="line">        &#123;</span><br><span class="line">          <span class="comment">// 当 /user/:id/profile 匹配成功，</span></span><br><span class="line">          <span class="comment">// UserProfile 会被渲染在 User 的 &lt;router-view&gt; 中</span></span><br><span class="line">          path: <span class="string">&#x27;profile&#x27;</span>,</span><br><span class="line">          component: UserProfile</span><br><span class="line">        &#125;,</span><br><span class="line">      ]</span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
</li>
<li><p>实例化VueRouter并暴露出去</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</span><br><span class="line">    routes</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> router</span><br></pre></td></tr></table></figure>
<h3 id="在main-js中引入使用router"><a href="#在main-js中引入使用router" class="headerlink" title="在main.js中引入使用router"></a>在main.js中引入使用router</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引入router</span></span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">&#x27;./router&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  <span class="comment">// 在vue实例化中使用router </span></span><br><span class="line">  router,</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2></li>
</ol>
<ul>
<li>声明式方法<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 路由配置的区域</span></span><br><span class="line">&lt;router-link to=<span class="string">&quot;/nav&quot;</span>&gt;nav&lt;/router-link&gt;</span><br><span class="line">&lt;router-link to=<span class="string">&quot;/user/123&quot;</span> &gt;user&lt;/router-link&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 路由需要配置参数 可以通过以下两种方法</span></span><br><span class="line">&lt;router-link</span><br><span class="line">  :to=<span class="string">&quot;&#123;</span></span><br><span class="line"><span class="string">  name:&#x27;tab&#x27;, // index配置的时候就得添加name</span></span><br><span class="line"><span class="string">  query:&#123;</span></span><br><span class="line"><span class="string">    name:&#x27;doreen&#x27;</span></span><br><span class="line"><span class="string">  &#125;,    // 不会刷新 /tab?name=doreen</span></span><br><span class="line"><span class="string">  params:&#123;</span></span><br><span class="line"><span class="string">    id:&#x27;123&#x27; // 会刷新 /tab/123</span></span><br><span class="line"><span class="string">  &#125;</span></span><br><span class="line"><span class="string">&#125;&quot;</span></span><br><span class="line">&gt;tab&lt;/router-link&gt;</span><br><span class="line"></span><br><span class="line">&lt;router-link :to=<span class="string">&quot;&#123; path:&#x27;/tab/&#x27;+id &#125;&quot;</span> &gt;user&lt;/router-link&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 组件内容的显示区域 </span></span><br><span class="line">&lt;router-view /&gt;</span><br></pre></td></tr></table></figure></li>
<li>编程式导航<blockquote>
<p>path和params一起写的时候params不生效！</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 字符串</span></span><br><span class="line">router.push(<span class="string">&#x27;home&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 对象</span></span><br><span class="line">router.push(&#123; <span class="attr">path</span>: <span class="string">&#x27;home&#x27;</span> &#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 命名的路由</span></span><br><span class="line">router.push(&#123; <span class="attr">name</span>: <span class="string">&#x27;user&#x27;</span>, <span class="attr">params</span>: &#123; <span class="attr">userId</span>: <span class="string">&#x27;123&#x27;</span> &#125;&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 带查询参数，变成 /register?plan=private</span></span><br><span class="line">router.push(&#123; <span class="attr">path</span>: <span class="string">&#x27;register&#x27;</span>, <span class="attr">query</span>: &#123; <span class="attr">plan</span>: <span class="string">&#x27;private&#x27;</span> &#125;&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在浏览器记录中前进一步，等同于 history.forward()</span></span><br><span class="line">router.go(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 后退一步记录，等同于 history.back()</span></span><br><span class="line">router.go(<span class="number">-1</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 前进 3 步记录</span></span><br><span class="line">router.go(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果 history 记录不够用，那就默默地失败呗</span></span><br><span class="line">router.go(<span class="number">-100</span>)</span><br><span class="line">router.go(<span class="number">100</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
</ul>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-vue-property-decorator"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/10/01/vue-property-decorator/"
    >vue-property-decorator的简单使用</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/10/01/vue-property-decorator/" class="article-date">
  <time datetime="2020-10-01T03:37:33.620Z" itemprop="datePublished">2020-10-01</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/vue/">vue</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="vue-property-decorator"><a href="#vue-property-decorator" class="headerlink" title="vue-property-decorator"></a><a target="_blank" rel="noopener" href="https://github.com/kaorun343/vue-property-decorator">vue-property-decorator</a></h2><blockquote>
<p>提供给vue内写typescript语法的时候引入的组件（基于vue-class-component）  </p>
</blockquote>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><blockquote>
<p>npm i -s vue-property-decorator</p>
</blockquote>
<h2 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</h2><h3 id="Props"><a href="#Props" class="headerlink" title="@Props"></a>@Props</h3><blockquote>
<p>props是子组件用来接收父组件传递过来的参数  </p>
</blockquote>
<p>在常规的vue语法环境下的书写方式：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    props: &#123;</span><br><span class="line">        propA: &#123;</span><br><span class="line">            type: <span class="built_in">Number</span>,</span><br><span class="line">            <span class="comment">// type: [ String,Boolean ]</span></span><br><span class="line">            <span class="comment">// default: &#x27;value&#x27;</span></span><br><span class="line">            <span class="comment">// default: function ()&#123; return &#x27;hello&#x27; &#125;</span></span><br><span class="line">            <span class="comment">// required: true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<br/>
<br/>
在vue-property-decorator组件中使用：  

<blockquote>
<p>@Prop( options: (PropOptions | Constructor[] | Constructor )={} )  </p>
</blockquote>
<p><code>@Prop</code>装饰器接收一个参数，这个参数的三种写法：  </p>
<ul>
<li><code>constructor</code> String、Number、Boolean等，指定prop的类型</li>
<li><code>constructor[]</code> 指定prop的可选类型</li>
<li><code>propOptions</code> 对象参数可以是 type，default，required…</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;script lang=<span class="string">&#x27;ts&#x27;</span>&gt;</span><br><span class="line">    <span class="keyword">import</span> &#123; Vue,Component,Prop &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Component</span>(&#123;&#125;) <span class="comment">// 不管有没有组件都必须写</span></span><br><span class="line">    <span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> 组件名 <span class="keyword">extends</span> Vue&#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Props</span>(<span class="built_in">Number</span>) propA!: <span class="built_in">number</span>;</span><br><span class="line">        <span class="meta">@Props</span>([<span class="built_in">String</span>,<span class="built_in">Boolean</span>]) propB: <span class="built_in">string</span> | <span class="built_in">boolean</span>;</span><br><span class="line">        <span class="meta">@Props</span>(&#123;<span class="keyword">default</span>:<span class="string">&#x27;value&#x27;</span>&#125;) propC!: <span class="built_in">string</span>;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>!</code> 表示一定有值(非<code>null</code>和非<code>undefined</code>)， <code>?</code> 表示可选  </p>
</blockquote>
<h3 id="Component"><a href="#Component" class="headerlink" title="@Component"></a>@Component</h3><blockquote>
<p>用来接收一个对象作为参数，在对象中可以声明<code>components,filters,directives</code>等，也可以声明<code>computed,watch</code>等  </p>
</blockquote>
<p>在常规的vue语法环境下的书写方式：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> DetailTable <span class="keyword">from</span> <span class="string">&#x27;./components/detail-table.vue&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    components: &#123;</span><br><span class="line">        DetailTable</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<br/>
<br/>
在vue-property-decorator组件中使用： 

<blockquote>
<p>Component( options:ComponentOptions = {} ) </p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> DetailTable <span class="keyword">from</span> <span class="string">&#x27;./components/detail-table.vue&#x27;</span>;</span><br><span class="line"><span class="comment">// ... </span></span><br><span class="line"><span class="meta">@Component</span>(&#123;</span><br><span class="line">    components:&#123;</span><br><span class="line">        DetailTable</span><br><span class="line">    &#125;,</span><br><span class="line">    filters: &#123;</span><br><span class="line">        toFixed: <span class="function">()=&gt;</span>&#123; ... &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> 组件名 <span class="keyword">extends</span> Vue &#123;</span><br><span class="line">    <span class="comment">// ... </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Watch"><a href="#Watch" class="headerlink" title="@Watch"></a>@Watch</h3><blockquote>
<p>侦听数据的变化  </p>
</blockquote>
<p>在常规的vue语法环境下的书写方式：  </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">watch: &#123;</span><br><span class="line">    msg: [</span><br><span class="line">        &#123;</span><br><span class="line">            handler: <span class="string">&#x27;onMsgChange&#x27;</span>,</span><br><span class="line">            immediate: <span class="literal">false</span>,</span><br><span class="line">            deep: <span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">      onMsgChange(newValue,oldValue) &#123; &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>在vue-property-decorator组件中使用：  </p>
<blockquote>
<p>@Watch( path: string, options: WatchOptions={} )  </p>
</blockquote>
<p><code>@Watch</code>装饰器接收两个参数：  </p>
<ul>
<li><code>path：string</code> 被侦听的属性名</li>
<li><code>options?: WatchOptions=&#123;&#125; options</code> 可以包含两个属性：<ul>
<li><code>immediate?: boolean</code> 侦听开始之后是否立即调用该回调函数；</li>
<li><code>deep?: boolean</code> 被侦听的对象的属性被改变时，是否调用该回调函数；<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> 组件名 <span class="keyword">extends</span> Vue &#123;</span><br><span class="line">    <span class="meta">@Watch</span>(<span class="string">&#x27;msg&#x27;</span>)</span><br><span class="line">    <span class="keyword">public</span> onMsgChange (newValue: <span class="built_in">string</span>, oldValue: <span class="built_in">string</span>) &#123;</span><br><span class="line">        <span class="comment">// ... </span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ul>
<h2 id="Emit"><a href="#Emit" class="headerlink" title="@Emit"></a>@Emit</h2><blockquote>
<p>广播方法获取变量</p>
</blockquote>
<p>在常规的vue语法环境下的书写方式：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    data ()&#123;</span><br><span class="line">        count: <span class="number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">    methods: &#123;</span><br><span class="line">        addToCount(n) &#123;</span><br><span class="line">            <span class="built_in">this</span>.count += n</span><br><span class="line">            <span class="built_in">this</span>.$emit(<span class="string">&#x27;add-to-count&#x27;</span>,n)</span><br><span class="line">        &#125;,</span><br><span class="line">        resetCount() &#123;</span><br><span class="line">            <span class="built_in">this</span>.count = <span class="number">0</span></span><br><span class="line">            <span class="built_in">this</span>.$emit(<span class="string">&#x27;reset&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>@Emit(event?:string) </p>
</blockquote>
<p><code>@Emit</code>装饰器接收一个可选参数，作为事件名<br><code>@Emit</code>将回调函数的返回值作为第二个参数  </p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> 组件名 <span class="keyword">extends</span> Vue&#123;</span><br><span class="line">    count = <span class="number">0</span></span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Emit</span>()</span><br><span class="line">    <span class="keyword">public</span> addToCount(n:<span class="built_in">number</span>)&#123;</span><br><span class="line">        <span class="built_in">this</span>.count += n</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Emit</span>(<span class="string">&#x27;reset&#x27;</span>)</span><br><span class="line">    <span class="keyword">public</span> resetCount() &#123;</span><br><span class="line">        <span class="built_in">this</span>.count = <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-vue-class-component"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/10/01/vue-class-component/"
    >vue-class-component的简单使用</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/10/01/vue-class-component/" class="article-date">
  <time datetime="2020-10-01T03:37:33.616Z" itemprop="datePublished">2020-10-01</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/vue/">vue</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="vue-class-component"><a href="#vue-class-component" class="headerlink" title="vue-class-component"></a><a target="_blank" rel="noopener" href="https://class-component.vuejs.org/">vue-class-component</a></h2><blockquote>
<p>vue对typescript支持的库</p>
</blockquote>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><blockquote>
<p>npm install –save vue vue-class-component </p>
</blockquote>
<h2 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</h2><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;script lang=<span class="string">&#x27;ts&#x27;</span>&gt;</span><br><span class="line"><span class="keyword">import</span> &#123; Component,Provide,Vue &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; BaseAxios &#125; <span class="keyword">from</span> <span class="string">&#x27;../api/axios&#x27;</span> </span><br><span class="line"><span class="keyword">import</span> input <span class="keyword">from</span> <span class="string">&#x27;components/input.vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 组件的引入</span></span><br><span class="line"><span class="meta">@Component</span>(&#123;</span><br><span class="line">    components: &#123; input &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">class</span> Hello <span class="keyword">extends</span> Vue &#123;</span><br><span class="line">    <span class="comment">// data </span></span><br><span class="line">    obj: <span class="built_in">Object</span> = &#123;&#125;</span><br><span class="line">    msg: <span class="built_in">string</span> = <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line">    num: <span class="built_in">number</span> = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// methods</span></span><br><span class="line">    addTimes (a: <span class="built_in">number</span>) :<span class="built_in">number</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.num += a</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.num </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// computed</span></span><br><span class="line">    get computedMsg() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;computed&#x27;</span> + msg</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 生命周期</span></span><br><span class="line">    mounted() &#123;</span><br><span class="line">        BaseAxios.post(<span class="string">&#x27;...&#x27;</span>).then( <span class="function"><span class="params">res</span>=&gt;</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(res)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h3 id="createDecorator"><a href="#createDecorator" class="headerlink" title="createDecorator"></a>createDecorator</h3><blockquote>
<p>自定义装饰器，接收一个回调函数作为第一个参数</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// decorator.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; createDecorator &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-class-component&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> NoCache = createDecorator(<span class="function">(<span class="params">options,key</span>)=&gt;</span>&#123;</span><br><span class="line">    options.computed[key].cache = <span class="literal">false</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// MyComp.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; NoCache &#125; <span class="keyword">from</span> <span class="string">&#x27;./decorators&#x27;</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">class</span> MyComp <span class="keyword">extends</span> Vue &#123;</span><br><span class="line">    <span class="comment">// computed属性不会被缓存</span></span><br><span class="line">    <span class="meta">@NoCache</span></span><br><span class="line">    get random() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Math</span>.random()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Component-registerHooks"><a href="#Component-registerHooks" class="headerlink" title="Component.registerHooks"></a>Component.registerHooks</h3><blockquote>
<p>自定义钩子</p>
</blockquote>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// class-component-hooks.js</span></span><br><span class="line"><span class="keyword">import</span> Component <span class="keyword">from</span> <span class="string">&#x27;vue-class-component&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 注册钩子name</span></span><br><span class="line">Component.registerHooks([</span><br><span class="line">    <span class="string">&#x27;beforeRouterEnter&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;beforeRouterLeave&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;beforeRouterUpdate&#x27;</span>,</span><br><span class="line">])</span><br><span class="line"></span><br><span class="line"><span class="comment">// MyComp.js</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Component <span class="keyword">from</span> <span class="string">&#x27;vue-class-component&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">class</span> MyComp <span class="keyword">extends</span> Vue &#123;</span><br><span class="line">    <span class="comment">// 在组件内部为注册的钩子函数实现原型方法</span></span><br><span class="line">    beforeRouterEnter(to,<span class="keyword">from</span>,next) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;before-router-enter&#x27;</span>)</span><br><span class="line">        next()</span><br><span class="line">    &#125;</span><br><span class="line">    beforeRouterLeave(to,<span class="keyword">from</span>,next) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;before-router-leave&#x27;</span>)</span><br><span class="line">        next()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-typescript"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/10/01/typescript/"
    >typescript基础语法</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/10/01/typescript/" class="article-date">
  <time datetime="2020-10-01T03:37:33.613Z" itemprop="datePublished">2020-10-01</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/typescript/">typescript</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <img src='/images/ts.png' />

<h2 id="1-1-简介"><a href="#1-1-简介" class="headerlink" title="1.1 简介"></a>1.1 简介</h2><ul>
<li><p>ts 是由微软开发的开源的编程语言</p>
</li>
<li><p><code>typescript</code>是<code>javascript</code>的超集</p>
</li>
<li><p>ts 是开发大型应用的基石</p>
</li>
<li><p>ts 提供了更丰富的语法提示</p>
</li>
<li><p>ts 在编写阶段能够检查错误</p>
</li>
</ul>
<h2 id="2-1-ts-是静态类型-js-是动态类型"><a href="#2-1-ts-是静态类型-js-是动态类型" class="headerlink" title="2.1 ts 是静态类型 , js 是动态类型"></a>2.1 ts 是静态类型 , js 是动态类型</h2><blockquote>
<p>动态类型<code>运行时</code>，解析器基于变量的类型决定变量的类型 （可以不需要提前声明变量类型）</p>
</blockquote>
<h2 id="3-1-数据类型"><a href="#3-1-数据类型" class="headerlink" title="3.1 数据类型"></a>3.1 数据类型</h2><ul>
<li><p>原始数据类型： <code>boolean</code> <code>string</code> <code>number</code> <code>null</code> <code>undefined</code> <code>symbol</code></p>
</li>
<li><p>引用数据类型： <code>object</code></p>
</li>
<li><p>基础类型：<code>boolean</code> <code>string</code> <code>number</code> <code>null</code> <code>undefined</code> <code>symbol</code> <code>any</code> <code>never</code> <code>void</code></p>
<ul>
<li><code>any</code> 没办法有代码提示;</li>
<li><code>any</code> 在代码测试的时候用;</li>
</ul>
<ul>
<li>对象 <code>interface</code></li>
<li>数组 <code>number[]</code>  <code>string[]</code> <code>boolean[]</code>  泛型的写法：<code> Array&lt;number&gt;</code></li>
</ul>
<p>函数的注解：</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> test = <span class="function"><span class="keyword">function</span> (<span class="params">a: <span class="built_in">number</span>, b: <span class="built_in">number</span></span>): <span class="title">number</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> a + b</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>新的语法特性：<br> 值 <code>as</code> 类型 ；或者 &lt;类型 &gt;值  // 断言<br> <code>class</code> (OOP 面向对象的三大特性) ： 封装 , 继承, 多态;</p>
</li>
</ul>
<h2 id="4-1-原始数据的注解"><a href="#4-1-原始数据的注解" class="headerlink" title="4.1 原始数据的注解"></a>4.1 原始数据的注解</h2><ul>
<li><p>布尔值的注解：</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> isDone: <span class="built_in">boolean</span> = <span class="literal">false</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>数字的注解：</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> decLiteral: <span class="built_in">number</span> = <span class="number">6</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>字符串的注解:</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name: <span class="built_in">string</span> = <span class="string">&quot;bob&quot;</span>;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="5-1-原始数据的注解"><a href="#5-1-原始数据的注解" class="headerlink" title="5.1 原始数据的注解"></a>5.1 原始数据的注解</h2><p>  <code>any</code> 数据类型： 任意数据类型;</p>
<ol>
<li>如果是不同变量的话，可以是任意的数据类型：</li>
<li>如果是对象的话, <code>any</code> 不能够提示原有的属性和方法;</li>
<li>未给初始值的变量类型 为 <code>any</code>;</li>
</ol>
<h2 id="6-1-原始数据的注解"><a href="#6-1-原始数据的注解" class="headerlink" title="6.1 原始数据的注解"></a>6.1 原始数据的注解</h2><ul>
<li><p><code>void</code>  当一个函数没有返回值时</p>
</li>
<li><p><code>null</code> &amp;&amp; <code>undefined</code></p>
<ul>
<li><code>null</code>和<code>undefined</code>是所有类型的子类型</li>
<li>–strictNullChecks :  let num: number = undefined;</li>
</ul>
</li>
<li><p><code>never</code>  类型表示的是那些永不存在的值的类型;</p>
</li>
<li><p><code>object</code> 表示非原始类型</p>
</li>
</ul>
<h2 id="7-1-类型注解-amp-amp-类型推断"><a href="#7-1-类型注解-amp-amp-类型推断" class="headerlink" title="7.1 类型注解 &amp;&amp; 类型推断"></a>7.1 类型注解 &amp;&amp; 类型推断</h2><h2 id="7-2-联合类型"><a href="#7-2-联合类型" class="headerlink" title="7.2 联合类型;"></a>7.2 联合类型;</h2><p>  注意：</p>
<ol>
<li><p>联合类型的共有属性是不会报错;</p>
</li>
<li><p>在赋值的时候确认类型;</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a: <span class="built_in">string</span> | <span class="built_in">number</span>;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="8-1-接口-interface"><a href="#8-1-接口-interface" class="headerlink" title="8.1 接口 interface"></a>8.1 接口 interface</h2><ol>
<li><p>对象的形状进行描述;</p>
</li>
<li><p>对类的一部分行为的抽象;</p>
<p><code>interface</code> 不可多，不可少;<br>可选属性：<code>age?: number</code>;<br>任意属性：<code>[propName: string]: any</code><br>只读属性：<code>readonly</code></p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> Person &#123;</span><br><span class="line">  name: <span class="built_in">string</span>;</span><br><span class="line">  age?: <span class="built_in">number</span>;  <span class="comment">// 可选属性;</span></span><br><span class="line">  [propName: <span class="built_in">string</span>]: <span class="built_in">any</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="9-1数组的注解"><a href="#9-1数组的注解" class="headerlink" title="9.1数组的注解"></a>9.1数组的注解</h2><p>  写法方式 </p>
<ol>
<li><p><code>类型[]</code>  // number[]  (纯数字的数组， 没有长度限制) , push 非类型的数据，是进不去的;</p>
</li>
<li><p><code>Array&lt;类型&gt;</code>  Array<number></p>
</li>
<li><p><code>interface</code> 方式;</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> List &#123;</span><br><span class="line">  [index: <span class="built_in">number</span>]: <span class="built_in">number</span> | <span class="built_in">string</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p><code>类数组</code></p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> Args &#123;</span><br><span class="line">  [index: <span class="built_in">number</span>]: <span class="built_in">any</span>;</span><br><span class="line">  length: <span class="built_in">number</span>;</span><br><span class="line">  callee: <span class="built_in">Function</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="10-1-函数的注解方式"><a href="#10-1-函数的注解方式" class="headerlink" title="10.1 函数的注解方式"></a>10.1 函数的注解方式</h2><ol>
<li><p>函数声明的注解方式;</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">test</span>(<span class="params">a: <span class="built_in">number</span>, b: <span class="built_in">number</span></span>): <span class="title">number</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> a + b</span><br><span class="line">  <span class="comment">// console.log(1)</span></span><br><span class="line">  <span class="comment">// throw new Error()</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>函数表达式的注解方式;</p>
</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> test1: (a: <span class="built_in">number</span>, b: <span class="built_in">number</span>):&#123;&#125; = <span class="function"><span class="keyword">function</span> (<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> &#123; a: <span class="number">1</span> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="10-2-函数的重载"><a href="#10-2-函数的重载" class="headerlink" title="10.2 函数的重载"></a>10.2 函数的重载</h2></li>
</ol>
<ul>
<li><p>相同的函数因为传入的参数类型不同 返回的值的类型也不同</p>
</li>
<li><p>表意更清楚;</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">reverse</span>(<span class="params">x: <span class="built_in">string</span></span>): <span class="title">string</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">reverse</span>(<span class="params">x: <span class="built_in">number</span></span>): <span class="title">number</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">reverse</span>(<span class="params">x: <span class="built_in">string</span> | <span class="built_in">number</span></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> x === <span class="string">&#x27;string&#x27;</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> x.split(<span class="string">&#x27;&#x27;</span>).reverse().join(<span class="string">&#x27;&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> x === <span class="string">&#x27;number&#x27;</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">Number</span>(x.toString().split(<span class="string">&#x27;&#x27;</span>).reverse().join(<span class="string">&#x27;&#x27;</span>))</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="11-1-类"><a href="#11-1-类" class="headerlink" title="11.1 类"></a>11.1 类</h2><p> <code> &quot;strictPropertyInitialization&quot;: false,</code></p>
<p>  类的注解方式：</p>
  <figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 父类 / 基类</span></span><br><span class="line"><span class="keyword">class</span> Animal &#123;</span><br><span class="line">  name: <span class="built_in">string</span>;</span><br><span class="line">  <span class="keyword">constructor</span>(<span class="params">theName: <span class="built_in">string</span></span>) &#123; <span class="built_in">this</span>.name = theName; &#125;</span><br><span class="line">  move(distanceInMeters: <span class="built_in">number</span> = <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span> moved <span class="subst">$&#123;distanceInMeters&#125;</span>m.`</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Cat <span class="keyword">extends</span> Animal &#123;</span><br><span class="line">  <span class="keyword">constructor</span>(<span class="params">name:<span class="built_in">string</span></span>)&#123;<span class="built_in">super</span>(name)&#125; <span class="comment">// 1</span></span><br><span class="line">  move(position:<span class="built_in">number</span>)&#123;</span><br><span class="line">    <span class="built_in">super</span>.move(position) <span class="comment">// 2</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li><strong><em>1-&gt;继承中的<code>super</code>指的是<code>构造函数</code></em></strong></li>
<li><strong><em>2-&gt;构造函数以外的<code>super</code>指的是<code>父类</code> ； 可以调用父类的属性和方法</em></strong></li>
</ul>
<h2 id="11-2-类的修饰符"><a href="#11-2-类的修饰符" class="headerlink" title="11.2 类的修饰符"></a>11.2 类的修饰符</h2><ul>
<li><p><code>public</code> 公共的成员属性 (默认)：</p>
<ol>
<li>自身调用;</li>
<li>子类可以调用;</li>
<li>实例调用;</li>
</ol>
</li>
<li><p><code>private</code> 私有属性：</p>
<ol>
<li>自身调用;</li>
</ol>
</li>
<li><p><code>protected</code> 受保护的成员属性： </p>
<ol>
<li>自身调用; </li>
<li>子类可以调用;</li>
</ol>
</li>
</ul>
<h2 id="11-3-类的修饰符-readonly"><a href="#11-3-类的修饰符-readonly" class="headerlink" title="11.3 类的修饰符 readonly;"></a>11.3 类的修饰符 readonly;</h2><ol>
<li><p>顺序  -&gt;  public readonly</p>
</li>
<li><p>是否可写： readonly 只读， 不可写</p>
</li>
<li><p>不能修饰成员方法;</p>
<p>参数属性： 简写的写法;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal1</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span>(private name: string) &#123; &#125;</span><br><span class="line">  move(distanceInMeters: number) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span> moved <span class="subst">$&#123;distanceInMeters&#125;</span>m.`</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> jake = <span class="keyword">new</span> Animal1(<span class="string">&#x27;jake&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(jake);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal2</span> </span>&#123;</span><br><span class="line">  private name: string;</span><br><span class="line">  <span class="keyword">constructor</span>(name: string) &#123; <span class="built_in">this</span>.name = name &#125;</span><br><span class="line">  move(distanceInMeters: number) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.name&#125;</span> moved <span class="subst">$&#123;distanceInMeters&#125;</span>m.`</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="11-4-类的存取器"><a href="#11-4-类的存取器" class="headerlink" title="11.4 类的存取器;"></a>11.4 类的存取器;</h2><ul>
<li><p><code>getter</code> 取值函数  obj.a</p>
</li>
<li><p><code>setter</code> 存值函数 obj.a = ‘123’</p>
<p>//同时出现, 就是一个属性;</p>
<p>改变赋值和读取的行为;</p>
</li>
</ul>
<h2 id="11-5-类的静态属性-amp-amp-抽象类"><a href="#11-5-类的静态属性-amp-amp-抽象类" class="headerlink" title="11.5 类的静态属性 &amp;&amp; 抽象类;"></a>11.5 类的静态属性 &amp;&amp; 抽象类;</h2><ul>
<li><p>抽象类： 能够提供其他类的基类;</p>
</li>
<li><p>抽象类： </p>
<ol>
<li>无法创建实例;</li>
<li>抽象方法一定要有实现;</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">      </span><br><span class="line">abstract <span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  abstract makeSound(): <span class="keyword">void</span>;</span><br><span class="line">  move(): <span class="keyword">void</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;roaming the earch...&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Snack</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  makeSound() &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;zzzzzzz&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  move(): <span class="keyword">void</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;roaming the earch...&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Rhino</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  makeSound() &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;mmmmmmm&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  move(): <span class="keyword">void</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;roaming the earch...&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Snack();</span><br><span class="line"></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="11-6-高阶技巧"><a href="#11-6-高阶技巧" class="headerlink" title="11.6 高阶技巧;"></a>11.6 高阶技巧;</h2><ol>
<li>定义类的时候 ， 定义了一个类型;</li>
<li>定义类的时候, 定义了一个构造函数;</li>
<li>接口可以继承类;</li>
</ol>
<h2 id="12-1-接口和-函数类型接口"><a href="#12-1-接口和-函数类型接口" class="headerlink" title="12.1 接口和 函数类型接口;"></a>12.1 接口和 函数类型接口;</h2><p>  函数类型接口：<br>  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">interface SearchFunc &#123;</span><br><span class="line">  (source: string, <span class="attr">subString</span>: string): boolean;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> mySearch: SearchFunc;</span><br><span class="line">mySearch = <span class="function"><span class="keyword">function</span> (<span class="params">source: string, subString: string</span>): <span class="title">boolean</span> </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> result = source.search(subString);</span><br><span class="line">  <span class="keyword">return</span> result &gt; <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type SearchFunc = <span class="function">(<span class="params">source: string, subString: string</span>) =&gt;</span> boolean</span><br><span class="line"></span><br><span class="line"><span class="comment">// function mySearch(source: string, subString: string): boolean &#123;</span></span><br><span class="line"><span class="comment">//   let result = source.search(subString);</span></span><br><span class="line"><span class="comment">//   return result &gt; -1;</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br></pre></td></tr></table></figure></p>
<h2 id="12-2-接口和-可索引的类型接口"><a href="#12-2-接口和-可索引的类型接口" class="headerlink" title="12.2 接口和 可索引的类型接口;"></a>12.2 接口和 可索引的类型接口;</h2><ol>
<li><p>共有支持两种索引签名：字符串和数字。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">interface NumberArray &#123;</span><br><span class="line">  [index: number]: number</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>是因为当使用number来索引时，<br>JavaScript会将它转换成string然后再去索引对象;</p>
</li>
<li><p>索引签名 有点像老大的意思;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">interface NumberDictionary &#123;</span><br><span class="line">  [index: string]: number | string;</span><br><span class="line">  length: number;    <span class="comment">// 可以，length是number类型</span></span><br><span class="line">  name: string       <span class="comment">// 错误，`name`的类型与索引类型返回值的类型不匹配</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>索引签名可以设置为只读;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">interface ReadonlyStringArray &#123;</span><br><span class="line">    readonly [index: number]: string;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> myArray: ReadonlyStringArray = [<span class="string">&quot;Alice&quot;</span>, <span class="string">&quot;Bob&quot;</span>];</span><br><span class="line">myArray[<span class="number">2</span>] = <span class="string">&quot;Mallory&quot;</span>; <span class="comment">// error!</span></span><br></pre></td></tr></table></figure>

<h2 id="12-3-类类型接口-–-类实现接口"><a href="#12-3-类类型接口-–-类实现接口" class="headerlink" title="12.3 类类型接口;   – 类实现接口;"></a>12.3 类类型接口;   – 类实现接口;</h2><p>对类的一部分行为的抽象;</p>
<p>类实现所有接口中的属性和方法; &amp;&amp; 对比 抽象类： 抽象方法需要实现;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">  </span><br><span class="line">interface ClockInterface &#123;</span><br><span class="line">    currentTime: <span class="built_in">Date</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Clock</span> <span class="title">implements</span> <span class="title">ClockInterface</span> </span>&#123;</span><br><span class="line">    currentTime: <span class="built_in">Date</span>;</span><br><span class="line">    <span class="keyword">constructor</span>(h: number, m: number) &#123; &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface Alarm &#123;</span><br><span class="line">  alert(): <span class="keyword">void</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface Light &#123;</span><br><span class="line">  color: string;</span><br><span class="line">  lightOn(): <span class="keyword">void</span>;  </span><br><span class="line">  lightOff(): <span class="keyword">void</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Door</span> </span>&#123; &#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SecurityDoor</span> <span class="keyword">extends</span> <span class="title">Door</span> <span class="title">implements</span> <span class="title">Alarm</span> </span>&#123;</span><br><span class="line">  alert() &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;hei&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="title">implements</span> <span class="title">Alarm</span>, <span class="title">Light</span> </span>&#123;</span><br><span class="line">  color = <span class="string">&#x27;red&#x27;</span>;</span><br><span class="line">  lightOn() &#123;</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">  lightOff() &#123;</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">  alert() &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;hello&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="12-4-类类型接口-–类静态部分与实例部分的区别"><a href="#12-4-类类型接口-–类静态部分与实例部分的区别" class="headerlink" title="12.4 类类型接口 –类静态部分与实例部分的区别"></a>12.4 类类型接口 –类静态部分与实例部分的区别</h2><p>  <strong><em><code>constructor</code>存在于类的静态部分，所以不在检查的范围内</em></strong></p>
<p>  类静态部分与实例部分需要单独做;</p>
  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 实例部分</span></span><br><span class="line">interface ClockInterface &#123;</span><br><span class="line">  currentTime: <span class="built_in">Date</span>;</span><br><span class="line">  getTime(h: number, <span class="attr">m</span>: number): any;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 构造函数 --静态类型部分</span></span><br><span class="line">interface ClockConstructor &#123;</span><br><span class="line">  <span class="keyword">new</span>(h: number, <span class="attr">m</span>: number): any</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Clock</span> <span class="title">implements</span> <span class="title">ClockInterface</span> </span>&#123;</span><br><span class="line">  currentTime = <span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line">  <span class="keyword">constructor</span>(h: number, m: number) &#123; &#125;</span><br><span class="line">  getTime() &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createClock</span>(<span class="params">C: ClockConstructor, h: number, m: number</span>) :<span class="title">ClockInterface</span></span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> C(h, m);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> clock = createClock(Clock, <span class="number">12</span>, <span class="number">12</span>);</span><br></pre></td></tr></table></figure>

<h2 id="12-5-接口继承接口-类实现接口"><a href="#12-5-接口继承接口-类实现接口" class="headerlink" title="12.5 接口继承接口 | 类实现接口;"></a>12.5 接口继承接口 | 类实现接口;</h2>  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"> interface Shape &#123;</span><br><span class="line">  color: string;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface PenStroke &#123;</span><br><span class="line">  penWidth: number;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface Square <span class="keyword">extends</span> Shape, PenStroke &#123;</span><br><span class="line">  sideLength: number;</span><br><span class="line">&#125;   </span><br></pre></td></tr></table></figure>
<h2 id="12-6-混合类型"><a href="#12-6-混合类型" class="headerlink" title="12.6 混合类型;"></a>12.6 混合类型;</h2><p>  函数类型的<code>interface</code>, 添加属性的方式来实现 对象的<code>interface</code>;</p>
  <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 函数类型 + 对象的类型</span></span><br><span class="line">  interface Counter &#123;</span><br><span class="line">    (start: number): string;</span><br><span class="line">    interval: number;</span><br><span class="line">    reset(): <span class="keyword">void</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">getCounter</span>(<span class="params"></span>): <span class="title">Counter</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 断言 =&gt; (&lt;any&gt;result).id =&gt; (result as any).id </span></span><br><span class="line">    <span class="comment">// 初始化值的时候使用</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> counter = <span class="xml"><span class="tag">&lt;<span class="name">Counter</span>&gt;</span>function(start: number) &#123; &#125;;</span></span><br><span class="line"><span class="xml">    counter.interval = 123;</span></span><br><span class="line"><span class="xml">    counter.reset = function () &#123; &#125;;</span></span><br><span class="line"><span class="xml">    return counter;</span></span><br><span class="line"><span class="xml">  &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="xml">  let c = getCounter();</span></span><br><span class="line"><span class="xml">  c(10);</span></span><br><span class="line"><span class="xml">  c.reset();</span></span><br><span class="line"><span class="xml">  c.interval = 5.0;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="12-7-混合类型的应用："><a href="#12-7-混合类型的应用：" class="headerlink" title="12.7 混合类型的应用："></a>12.7 混合类型的应用：</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">interface ClockInterface &#123;</span><br><span class="line">  currentTime: <span class="built_in">Date</span>;</span><br><span class="line">  getTime(h: number, <span class="attr">m</span>: number): any;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface ClockConstructor &#123;</span><br><span class="line">  <span class="keyword">new</span>(h: number, <span class="attr">m</span>: number): any;</span><br><span class="line">  getTime1(): <span class="keyword">void</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Clock</span> <span class="title">implements</span> <span class="title">ClockInterface</span> </span>&#123;</span><br><span class="line">  currentTime = <span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line">  <span class="keyword">constructor</span>(h: number, m: number) &#123; &#125;</span><br><span class="line">  getTime() &#123;&#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">static</span> getTime1() &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createClock</span>(<span class="params">C: ClockConstructor, h: number, m: number</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> C(h, m);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> clock = createClock(Clock, <span class="number">12</span>, <span class="number">12</span>);</span><br></pre></td></tr></table></figure>
<h2 id="12-8-接口继承类："><a href="#12-8-接口继承类：" class="headerlink" title="12.8 接口继承类："></a>12.8 接口继承类：</h2><ol>
<li>类可以实现接口;</li>
<li>接口可以继承接口;</li>
<li>接口可以继承类;</li>
</ol>
<h2 id="13-1-泛型"><a href="#13-1-泛型" class="headerlink" title="13.1 泛型"></a>13.1 泛型</h2><p> <strong>*<code>T</code> 泛型变量 | 类型变量 *</strong></p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">identity1</span>&lt;<span class="title">T</span>&gt;(<span class="params">arg: T</span>): <span class="title">T</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> arg;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">identity1(<span class="string">&#x27;123&#x27;</span>)</span><br><span class="line">identity1&lt;number&gt;(<span class="number">123</span>)</span><br><span class="line"></span><br><span class="line">interface MyArray&lt;T&gt; &#123;</span><br><span class="line">  [n: number]: T;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> a: number[] = [<span class="number">12</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line"><span class="keyword">let</span> b: <span class="built_in">Array</span>&lt;number&gt; = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br></pre></td></tr></table></figure>

<h2 id="13-2-泛型类型"><a href="#13-2-泛型类型" class="headerlink" title="13.2 泛型类型"></a>13.2 泛型类型</h2> <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">identity</span>&lt;<span class="title">U</span>&gt;(<span class="params">arg: U</span>): <span class="title">U</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> arg;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface IdentityInterface&lt;T&gt; &#123;</span><br><span class="line">  (arg: T): T</span><br><span class="line">&#125;</span><br><span class="line">interface IdentityInterface &#123;</span><br><span class="line">  &lt;T&gt;(arg: T): T</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol>
<li><p>函数泛型的注解方式；</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a: <span class="xml"><span class="tag">&lt;<span class="name">T</span>&gt;</span>(arg: T) =&gt; T = identity;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>对象字面量的方式来定义泛型类型;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a: &#123; <span class="xml"><span class="tag">&lt;<span class="name">T</span>&gt;</span>(arg: T): T &#125; = identity;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>泛型接口的定义方式：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a: IdentityInterface = identity;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="13-3-泛型类-泛型约束"><a href="#13-3-泛型类-泛型约束" class="headerlink" title="13.3 泛型类 泛型约束;"></a>13.3 泛型类 泛型约束;</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MinClass</span>&lt;<span class="title">T</span>&gt;</span>&#123;</span><br><span class="line">  public list: T[] = [];</span><br><span class="line">  add(num: T) &#123;</span><br><span class="line">    <span class="built_in">this</span>.list.push(num);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  min(): T &#123;</span><br><span class="line">    <span class="keyword">let</span> minNum = <span class="built_in">this</span>.list[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="built_in">this</span>.list.length; i++) &#123;</span><br><span class="line">      <span class="keyword">if</span> (minNum &gt; <span class="built_in">this</span>.list[i]) &#123;</span><br><span class="line">        minNum = <span class="built_in">this</span>.list[i];</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> minNum;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>泛型约束：<br><code>extends</code> 接口的方式(不一定是接口);</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">interface LenthWise1 &#123;</span><br><span class="line">  length: number;</span><br><span class="line">&#125;</span><br><span class="line">type LenthWise = string | LenthWise1</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">loggingIdentity</span>&lt;<span class="title">T</span> <span class="title">extends</span> <span class="title">LenthWise</span>&gt;(<span class="params">arg: T</span>): <span class="title">T</span> </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(arg.length);  <span class="comment">// Error: T doesn&#x27;t have .length</span></span><br><span class="line">  <span class="keyword">return</span> arg;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="13-4-多重泛型约束-amp-amp-amp"><a href="#13-4-多重泛型约束-amp-amp-amp" class="headerlink" title="13.4 多重泛型约束;   ||  &amp;&amp;   |  &amp;"></a>13.4 多重泛型约束;   ||  &amp;&amp;   |  &amp;</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">interface Sentence &#123;</span><br><span class="line">  content: string;</span><br><span class="line">  title: string;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface Music &#123;</span><br><span class="line">  url: string;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Test</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">Sentence</span> &amp; <span class="title">Music</span>&gt;</span>&#123;</span><br><span class="line">  props: T</span><br><span class="line">  <span class="keyword">constructor</span>(public arg: T) &#123;</span><br><span class="line">    <span class="built_in">this</span>.props = arg</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  info() &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      url: <span class="built_in">this</span>.props.url,</span><br><span class="line">      content: <span class="built_in">this</span>.props.content,</span><br><span class="line">      title: <span class="built_in">this</span>.props.title</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="13-5-在泛型里使用类类型-约束或是更好的推论"><a href="#13-5-在泛型里使用类类型-约束或是更好的推论" class="headerlink" title="13.5  在泛型里使用类类型, 约束或是更好的推论;"></a>13.5  在泛型里使用类类型, 约束或是更好的推论;</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">create</span>&lt;<span class="title">T</span>&gt;(<span class="params">c: &#123; new(): T; &#125;</span>): <span class="title">T</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> c();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BeeKeeper</span> </span>&#123;</span><br><span class="line">  hasMask: boolean;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ZooKeeper</span> </span>&#123;</span><br><span class="line">  nametag: string;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  numLegs: number;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Bee</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  keeper: BeeKeeper;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Lion</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  keeper: ZooKeeper;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createInstance</span>&lt;<span class="title">A</span> <span class="title">extends</span> <span class="title">Animal</span>&gt;(<span class="params">c: new () =&gt; A</span>): <span class="title">A</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> c();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">createInstance(Lion).keeper.nametag;  <span class="comment">// typechecks!</span></span><br><span class="line">createInstance(Bee).keeper.hasMask; </span><br></pre></td></tr></table></figure>
<h2 id="14-元组"><a href="#14-元组" class="headerlink" title="14  元组;"></a>14  元组;</h2><ol>
<li><p>确定成员 <code>类型</code>、<code>长度</code>;</p>
</li>
<li><p><code>push</code> 越界的时 , 类型为联合类型;</p>
</li>
<li><p>可选的元素类型：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> list: [number, string, boolean?];</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>rest</code> 拓展运算符;</p>
<pre><code class="javascript">declare function test(...args: [number, string, boolean]): void;
declare function test(arg1: number, arg2: string, arg3: boolean): void;

let list1: [number, ...string[]] = [1, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;];
let list2: [string, ...number[]] = [&#39;1&#39;, 2, 3, 4, 5, 6, 7];</code></pre>
</li>
</ol>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-next"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/05/25/next/"
    >Next.js</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/05/25/next/" class="article-date">
  <time datetime="2020-05-25T04:21:42.000Z" itemprop="datePublished">2020-05-25</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/react/">react</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <p><strong><a target="_blank" rel="noopener" href="https://www.nextjs.cn/docs/getting-started">next中文文档</a></strong>  </p>
<h2 id="next-js简介"><a href="#next-js简介" class="headerlink" title="next.js简介"></a>next.js简介</h2><blockquote>
<p>Next.js 是一个轻量级的 React 服务端渲染应用框架。</p>
</blockquote>
<ul>
<li>完善React项目架构，搭建轻松。比如：Webpack配置，服务器启动，路由配置，缓存能力，这些在它内部已经完善的为我们搭建完成了。</li>
<li>自带数据同步策略，解决服务端渲染最大难点。把服务端渲染好的数据，拿到客户端重用，这个在没有框架的时候，是非常复杂和困难的。有了Next.js，它为我们提供了非常好的解决方法，让我们轻松的就可以实现这些步骤。</li>
<li>丰富的插件帮开发人员增加各种功能。每个项目的需求都是不一样的，包罗万象。无所不有，它为我们提供了插件机制，让我们可以在使用的时候按需使用。你也可以自己写一个插件，让别人来使用。</li>
<li>灵活的配置，让开发变的更简单。它提供很多灵活的配置项，可以根据项目要求的不同快速灵活的进行配置。</li>
</ul>
<h2 id="创建next-js应用程序"><a href="#创建next-js应用程序" class="headerlink" title="创建next.js应用程序"></a>创建next.js应用程序</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 1.安装next的项目的脚手架工具 create-next-app</span><br><span class="line">npm install -g create-next-app</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 2.创建next项目</span><br><span class="line">npx create-next-app xxx</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="项目结构"><a href="#项目结构" class="headerlink" title="项目结构"></a>项目结构</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">xxx</span><br><span class="line">├── README.md</span><br><span class="line">├── node_modules</span><br><span class="line">├── package.json</span><br><span class="line">├── .gitignore</span><br><span class="line">├── public      &#x2F;&#x2F; 公共静态文件存放地</span><br><span class="line">├── components     &#x2F;&#x2F; 组件存放地</span><br><span class="line">└── page    &#x2F;&#x2F; 页面存放地，自动生成路由渲染在服务器</span><br></pre></td></tr></table></figure>
<h2 id="page的使用"><a href="#page的使用" class="headerlink" title="page的使用"></a>page的使用</h2><p>需要新添加路由则直接在page文件夹下创建新文件<br>eg：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">Index</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (<span class="xml"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>hello world<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>打开<code>http://localhost:3000/Index</code>即可访问到相关内容</p>
<h2 id="component的使用"><a href="#component的使用" class="headerlink" title="component的使用"></a>component的使用</h2><ol>
<li><p>以创建一个item组件为例</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (&#123;children&#125;)=&gt;&#123;</span><br><span class="line">     &lt;button&gt;&#123;children&#125;&lt;/button&gt;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// &#123;children&#125; =&gt; props.children 也可以在里面直接写props 获取的时候 props.children.xx</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在page文件夹下的页面中引入  </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Item <span class="keyword">from</span> <span class="string">&#x27;../components/item&#x27;</span></span><br></pre></td></tr></table></figure></li>
<li><p>使用组件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;Item&gt;按钮组件 / &#123;xx&#125;&lt;/Item&gt;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="路由相关"><a href="#路由相关" class="headerlink" title="路由相关"></a>路由相关</h2><blockquote>
<p>页面跳转一般有两种形式，第一种是利用标签<Link>,第二种是用js编程的方式进行跳转，也就是利用Router组件。  </p>
</blockquote>
<h3 id="标签式导航"><a href="#标签式导航" class="headerlink" title="标签式导航"></a>标签式导航<Link></h3><p>在主页面和可跳转页面都引入link</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// home-page</span></span><br><span class="line"><span class="keyword">import</span> Link <span class="keyword">from</span> <span class="string">&#x27;next/link&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Home=<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;h1&gt;首页&lt;/h1&gt;</span><br><span class="line">        &lt;Link href=<span class="string">&#x27;/pageA&#x27;</span>&gt;<span class="xml"><span class="tag">&lt;<span class="name">a</span>&gt;</span>A页面<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span>&lt;/Link&gt;</span><br><span class="line">        &#123;<span class="comment">/*  </span></span><br><span class="line"><span class="comment">            带参数传递</span></span><br><span class="line"><span class="comment">            &lt;Link href=&#x27;/pageA?id=123&#x27;&gt;&lt;a&gt;A页面&lt;/a&gt;&lt;/Link&gt; </span></span><br><span class="line"><span class="comment">            &lt;Link href=&#123;&#123;pathname:&#x27;/pageA&#x27;,query:&#123;id:&#x27;123&#x27;&#125;&#125;&#125;&gt;&lt;a&gt;A页面&lt;/a&gt;&lt;/Link&gt; </span></span><br><span class="line"><span class="comment">        */</span>&#125;</span><br><span class="line">        &lt;Link href=<span class="string">&#x27;/pageB&#x27;</span>&gt;<span class="xml"><span class="tag">&lt;<span class="name">a</span>&gt;</span>B页面<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span>&lt;/Link&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// ----------</span></span><br><span class="line"><span class="comment">// pageA</span></span><br><span class="line"><span class="keyword">import</span> Link <span class="keyword">from</span> <span class="string">&#x27;next/link&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (&#123;router&#125;)=&gt;&#123;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;h1&gt;page A&lt;/h1&gt;</span><br><span class="line">        &#123;<span class="comment">/* </span></span><br><span class="line"><span class="comment">            query接收参数</span></span><br><span class="line"><span class="comment">            &lt;span&gt;&#123;router.query.id&#125;&lt;/span&gt;</span></span><br><span class="line"><span class="comment">        */</span>&#125;</span><br><span class="line">        &lt;Link href=<span class="string">&#x27;/&#x27;</span>&gt;<span class="xml"><span class="tag">&lt;<span class="name">a</span>&gt;</span>返回首页<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span>&lt;/Link&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// -----------</span></span><br><span class="line"><span class="comment">// pageB</span></span><br><span class="line"><span class="keyword">import</span> Link <span class="keyword">from</span> <span class="string">&#x27;next/link&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> ()=&gt;&#123;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;h1&gt;page B&lt;/h1&gt;</span><br><span class="line">        &lt;Link href=<span class="string">&#x27;/&#x27;</span>&gt;<span class="xml"><span class="tag">&lt;<span class="name">a</span>&gt;</span>返回首页<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span>&lt;/Link&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Router模块进行跳转"><a href="#Router模块进行跳转" class="headerlink" title="Router模块进行跳转"></a>Router模块进行跳转</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Router <span class="keyword">from</span> <span class="string">&#x27;next/router&#x27;</span></span><br><span class="line"><span class="keyword">const</span> Home = <span class="function">()=&gt;</span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">gotoB</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    Router.push(<span class="string">&#x27;/pageB&#x27;</span>)</span><br><span class="line">    <span class="comment">/* </span></span><br><span class="line"><span class="comment">    带参数传递</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    Router.push(&#x27;/pageB?id=123&#x27;)   </span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    Router.push(&#123;</span></span><br><span class="line"><span class="comment">        pathname: &#x27;/pageB&#x27;,</span></span><br><span class="line"><span class="comment">        query: &#123;</span></span><br><span class="line"><span class="comment">            id: &#x27;123&#x27;</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">    &#125;)</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> (</span><br><span class="line">    ...</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;button onClick=&#123;<span class="function">()=&gt;</span>&#123;Router.push(<span class="string">&#x27;pageA&#x27;</span>)&#125;&#125;&gt;A页面&lt;/button&gt;</span><br><span class="line">        &lt;button onClick=&#123;gotoB&#125;&gt;B页面&lt;/button&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    ...</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="钩子函数"><a href="#钩子函数" class="headerlink" title="钩子函数"></a>钩子函数</h3><h4 id="routeChangeStart路由发生变化时"><a href="#routeChangeStart路由发生变化时" class="headerlink" title="routeChangeStart路由发生变化时"></a>routeChangeStart路由发生变化时</h4><h4 id="routerChangeComplete路由结束变化时"><a href="#routerChangeComplete路由结束变化时" class="headerlink" title="routerChangeComplete路由结束变化时"></a>routerChangeComplete路由结束变化时</h4><h4 id="beforeHistoryChange浏览器history触发前"><a href="#beforeHistoryChange浏览器history触发前" class="headerlink" title="beforeHistoryChange浏览器history触发前"></a>beforeHistoryChange浏览器history触发前</h4><h4 id="routeChangeError路由跳转发生错误时"><a href="#routeChangeError路由跳转发生错误时" class="headerlink" title="routeChangeError路由跳转发生错误时"></a>routeChangeError路由跳转发生错误时</h4><h4 id="hashChangeStart-hash开始跳转时"><a href="#hashChangeStart-hash开始跳转时" class="headerlink" title="hashChangeStart hash开始跳转时"></a>hashChangeStart hash开始跳转时</h4><h4 id="hashChangeComplete-hash完成跳转时"><a href="#hashChangeComplete-hash完成跳转时" class="headerlink" title="hashChangeComplete hash完成跳转时"></a>hashChangeComplete hash完成跳转时</h4><h2 id="getInitialProps数据获取"><a href="#getInitialProps数据获取" class="headerlink" title="getInitialProps数据获取"></a>getInitialProps数据获取</h2><p>配合axios  </p>
<ol>
<li>安装引入axios<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">yarn add axios</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在所需的页面引入axios</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">const</span> Home = <span class="function">(<span class="params">&#123;router,list&#125;</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;&gt;</span><br><span class="line">            &lt;div&gt;&#123;list&#125;&lt;/div&gt;</span><br><span class="line">        &lt;/&gt;</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> 通过getInitialProps获取数据</span></span><br><span class="line"><span class="comment"> 然后页面通过props.list获取数据，并进行显示</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">Home.getInitialProps = <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> res = <span class="keyword">await</span> axios(<span class="string">&#x27;http://rap2.taobao.org:38080/app/mock/250724/get/data&#x27;</span>).then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> res.data</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">//返回值只能是resolve对象</span></span><br><span class="line">  <span class="keyword">return</span> res</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withRouter(Home)</span><br></pre></td></tr></table></figure>
<h2 id="CSS编写"><a href="#CSS编写" class="headerlink" title="CSS编写"></a>CSS编写</h2></li>
</ol>
<ul>
<li>使用style jsx语法<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在return内部编写</span></span><br><span class="line">&lt;style jsx&gt;</span><br><span class="line">&#123;<span class="string">`</span></span><br><span class="line"><span class="string">    div&#123; color:red &#125;</span></span><br><span class="line"><span class="string">`</span>&#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure></li>
<li>引入CSS文件<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 安装配置相应内容</span></span><br><span class="line">yarn add @zeit/next=css</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在根目录建立next.config.js配置</span></span><br><span class="line"><span class="keyword">const</span> withCss = <span class="built_in">require</span>(<span class="string">&#x27;@zeit/next-css&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="built_in">require</span> !== <span class="string">&#x27;undefined&#x27;</span>)&#123;</span><br><span class="line">    <span class="built_in">require</span>.extensions[<span class="string">&#x27;.css&#x27;</span>]=<span class="function"><span class="params">file</span>=&gt;</span>&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = withCss(&#123;&#125;)</span><br></pre></td></tr></table></figure>
重启项目之后就可以进行使用<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在所需页面引入样式文件</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;../public/test.css&#x27;</span></span><br></pre></td></tr></table></figure>




</li>
</ul>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
    <article
  id="post-reactRedux"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2020/04/19/reactRedux/"
    >react-redux的简单使用</a> 
</h2>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/04/19/reactRedux/" class="article-date">
  <time datetime="2020-04-19T04:20:42.000Z" itemprop="datePublished">2020-04-19</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/react/">react</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>redux为react提供的一个专用库<br>react-redux组成：</p>
<ol>
<li><p>UI组件（presentational component）  </p>
<ul>
<li>只负责ui的呈现，不带有任何业务逻辑</li>
<li>没有状态（不使用this.state这个变量）</li>
<li>所有数据都由参数（this.props）提供</li>
<li>不使用任何Redux的API</li>
</ul>
</li>
<li><p>容器组件（container component）</p>
<ul>
<li>负责管理数据和业务逻辑，不负责UI的呈现</li>
<li>带有内部状态</li>
<li>使用Redux的API</li>
</ul>
</li>
</ol>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><blockquote>
<p>npm i react-redux –save </p>
</blockquote>
<p>配合redux使用</p>
<blockquote>
<p>npm i redux –save</p>
</blockquote>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><h3 id="全局配置"><a href="#全局配置" class="headerlink" title="全局配置"></a>全局配置</h3><p>利用<code>&lt;Provider&gt;</code>组件来实现容器组件获取<code>state</code>对象<br>在src下的index.js全局进行配置</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">&#x27;react-dom&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;Provider&#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;./store&#x27;</span></span><br><span class="line"><span class="keyword">const</span> app = (</span><br><span class="line">    &lt;Provider store=&#123;store&#125;&gt;    <span class="comment">// 让App所有的子组件都默认获取到了state</span></span><br><span class="line">        &lt;App/&gt;</span><br><span class="line">    &lt;/Provider&gt;</span><br><span class="line">)</span><br><span class="line"><span class="comment">//ReactDOM.render(&lt;App /&gt;,document.getElementById(&#x27;root&#x27;));</span></span><br><span class="line">ReactDOM.render(app,<span class="built_in">document</span>.getElementById(<span class="string">&#x27;root&#x27;</span>));</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="组件配置"><a href="#组件配置" class="headerlink" title="组件配置"></a>组件配置</h3><p>connect方法用于从<code>UI组件</code>生成<code>容器组件</code>。<br>在组件内部引入connect方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React, &#123; Component &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 将app转化为UI组件的形式进行展示</span></span><br><span class="line"><span class="keyword">const</span> App = <span class="function">(<span class="params">props</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">let</span> &#123;inputValue,add,list,changeInput&#125; = props <span class="comment">// 从属性中获取到方法和对象</span></span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">        &lt;input type=<span class="string">&quot;text&quot;</span> placeholder=&#123;inputValue&#125; onChange=&#123;changeInput&#125; /&gt;</span><br><span class="line">        &lt;button onClick=&#123;add&#125;&gt;提交&lt;/button&gt;</span><br><span class="line">        &lt;ul&gt;</span><br><span class="line">          &#123;</span><br><span class="line">            list.map(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">              <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">li</span> <span class="attr">key</span>=<span class="string">&#123;index&#125;</span>&gt;</span>&#123;item&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line">            &#125;)</span><br><span class="line">          &#125;</span><br><span class="line">        &lt;/ul&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>connect方法接收两个参数：</p>
<ol>
<li>mapStateToProps</li>
<li>mapDispatchToProps<br>它们定义了ui组件的业务逻辑<br>前者负责输入逻辑 =&gt; state映射到ui组件的参数（props）<br>后者负责输出逻辑 =&gt; 用户对ui组件的操作映射成Action   </li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 映射关系</span></span><br><span class="line"><span class="keyword">const</span> stateToProps = <span class="function">(<span class="params">state</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    inputValue: state.inputValue,</span><br><span class="line">    list: state.list</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> dispatchToProps = <span class="function">(<span class="params">dispatch</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    changeInput(e)&#123;</span><br><span class="line">      <span class="keyword">let</span> action = &#123;</span><br><span class="line">        type: <span class="string">&#x27;changeInput&#x27;</span>,</span><br><span class="line">        value: e.target.value</span><br><span class="line">      &#125;</span><br><span class="line">      dispatch(action)</span><br><span class="line">    &#125;,</span><br><span class="line">    add()&#123;</span><br><span class="line">      <span class="keyword">let</span> action = &#123;</span><br><span class="line">        type: <span class="string">&#x27;add&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">      dispatch(action)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(stateToProps, dispatchToProps)(App);</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
    </footer>
  </div>

    
 
   
</article>

    
  </article>
  

  
  <nav class="page-nav">
    
    <a class="extend prev" rel="prev" href="/page/3/">上一页</a><a class="page-number" href="/">1</a><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><span class="page-number current">4</span><a class="page-number" href="/page/5/">5</a><a class="page-number" href="/page/6/">6</a><a class="page-number" href="/page/7/">7</a><a class="extend next" rel="next" href="/page/5/">下一页</a>
  </nav>
  
</section>
</div>

      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2019-2023
        <i class="ri-heart-fill heart_icon"></i> John Doe
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.webp" alt="Doreen&#39;s Blog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>